![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
Sending keys(presses) in Windows
I'm working on a small program that will send keys to a program.. Well whatever is active atm on a mouseclick.. But lets say the program I'm sending to has a function that is triggered when space " " is pressed and I want to bind it to mouse click in my program, sending a space doesn't work, I thought it wouldn't but was worth a try
~~ So is there anyway to actually emulate pressing a key in the program? c++ Syntax (Toggle Plain Text)
c++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 |
|
Expert Bug Developer
Join Date: Apr 2008
Posts: 21
Rep Power: 0
![]() |
Re: Sending keys(presses) in Windows
If I recall correctly, SendInput() only affects the application in the foreground. Thus, if you want this to work, you'll have to set the focus of the application after you receive the event for the mouse click. I haven't programmed on Windows in a long long time, but I belive you can use the SetFocus() function to accomplish this. You'll need a window handle though. Then again, If you have the window handle, you might as well just send the message directly to the window with SendMessage.
It's also quite likely that I'm completely wrong and you should ignore me entirely. Do whatever you wish with the information I provided. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2005
Posts: 110
Rep Power: 4
![]() |
Re: Sending keys(presses) in Windows
yeah I already had focus and the window handle i didn't post the whole code cause it was quite big, anyway I will look up SendMessage and see if that helps, thanks ^^
|
|
|
|
|
|
#4 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
Re: Sending keys(presses) in Windows
You don't have to keep the window focused to let it receive messages. By modifying this function you will be able to simulate key presses in any window that is running.
void send_key(UINT key)
{
HWND window = FindWindow("#32770", "app.exe"); // TODO: <-- MODIFY THIS TO GET THE CORRECT WINDOW HANDLE
typedef struct tag_KEYDATA
{
WORD cRepeat;
BYTE ScanCode;
BYTE fExtended;
BYTE Reserved : 4;
BYTE fAltDown : 1;
BYTE fRepeat : 1;
BYTE fUp : 1;
}KEYDATA;
KEYDATA sKey;
memset(&sKey, 0, sizeof(KEYDATA));
sKey.cRepeat = 1;
sKey.ScanCode = (BYTE)MapVirtualKey(key, 0);
sKey.fExtended = 0;
sKey.fAltDown = 0;
sKey.fRepeat = 0;
sKey.fUp = 0;
DWORD dwVal_down;
memcpy(&dwVal_down, &sKey, sizeof(DWORD));
DWORD dwVal_up;
memcpy(&dwVal_up, &sKey, sizeof(DWORD));
PostMessage(window, WM_KEYDOWN, key, dwVal_down);
Sleep(50);
PostMessage(window, WM_KEYUP, key, dwVal_up | 0xc0000000);
}/Klarre
__________________
http://www.klarre.se |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Windows Installer Service Problems | Mjordan2nd | Coder's Corner Lounge | 2 | Jan 25th, 2008 2:01 PM |
| Windows XP Activation Loop... | Ghost | Coder's Corner Lounge | 3 | Jan 24th, 2008 3:28 PM |
| looking for a good windows programming book | cwl157 | Coder's Corner Lounge | 3 | May 30th, 2007 6:51 AM |
| Search for and close open windows | badbasser98 | C++ | 13 | Feb 27th, 2006 12:04 PM |
| Net Group /ADD (on a windows box, non domain controller) | Infinite Recursion | Other Programming Languages | 1 | Apr 13th, 2005 2:27 PM |