Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 29th, 2008, 1:44 PM   #1
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
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)
  1. void send_key(BYTE vKey)
  2. {
  3.  
  4. INPUT Input;
  5. ZeroMemory(&Input, sizeof(Input));
  6. Input.type = INPUT_KEYBOARD;
  7. Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
  8. Input.ki.wVk = vKey;
  9. SendInput(1, &Input, sizeof(INPUT));
  10.  
  11. }

c++ Syntax (Toggle Plain Text)
  1. send_key(VK_SPACE);
cloud- is offline   Reply With Quote
Old May 29th, 2008, 6:51 PM   #2
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 21
Rep Power: 0 Sorrofix is on a distinguished road
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.
Sorrofix is offline   Reply With Quote
Old May 29th, 2008, 7:20 PM   #3
cloud-
Hobbyist Programmer
 
Join Date: Jan 2005
Posts: 110
Rep Power: 4 cloud- is on a distinguished road
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 ^^
cloud- is offline   Reply With Quote
Old May 30th, 2008, 12:26 PM   #4
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4 Klarre is on a distinguished road
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
Klarre is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC