Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use the spacebar event to pause a song . I wanted to use the keybd_event function to simulate the spacebar. I played an audio file in the background before running my program and if my code would have worked then the spacebar key would have paused the audio file. But nothing happened.
I am using Windows 8 64bit OS and i dont know if that has anything to do with my code but still i wanted to mention about it. Any suggestions or corrections will be really helpful.

This is what i tried

C++
#include <iostream>
#include <windows.h>
#include<stdio.h>

using namespace std;

int main()
{
	
        keybd_event(VK_SPACE,0,0,0); // Press Spacebar
        system("pause");    

	return 0;
}
Posted
Updated 16-Aug-14 3:48am
v2
Comments
Mohibur Rashid 18-Aug-14 0:01am    
did you try google?
by the way try this link:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx

1 solution

Seems like you're going to have a fight with windows over control of the keyboard input focus.

When you run your program - from a command line or through windows explorer, the keyboard focus moves to whatever window you used to run the program.

One the program starts, as a console program it will create it's own window and again take the keyboard input focus.

More than likely the program is sending the spacebar to itself.

Add code to your program to locate the top window of the application you want to control and set the focus to the other application - before sending a space bar.

To further reduce the intrusiveness of your program, rewrite it as WinMain() instead of main. Set the project as a Windowed subsystem application instead of a console application. A program that runs under WinMain and never creates a window can't take the input focus.

One minor suggestion - the scan code for the spacebar appears to 0x39. Probably not needed but doesn't hurt.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900