Re: Keypress simulation in Delphi
Posted by webmaster Guido on August 21, 2004 at 14:54:55:
In Reply to: Re: Keypress posted by Joey p12386 on August 21, 2004 at 10:39:32:
: I don't actually want to simulate it on a component i was just wondering if there was a way to do it (ie. A Windows Message). I found these but they dont seem to work :|
: keybd_event(vk_control, 1,0,0);
I just tested your code by copying it straight into a small test program, and it worked without any problems. I really can't guess what's wrong, so below I give some background info that might inspire you.
Actually, the function Keybd_Event is the one used in the "Send Keys" library, that I mentioned in my previous message. With Keybd_Event, you send keystrokes to the TOPMOST application window by stuffing key codes directly into Windows' keyboard buffer.
Attention: if you want to send something to another program's window, you firstly have to bring that window to the foreground. Otherwise the keys go to your own application window.
According to the Help files, the 4 parameters for KEYBD_EVENT(bVK, bScan, dwFlags, dwExtraInfo) are:
So, for each keystroke, you firstly specify which key needs to be pressed, next you specify to release that key, for example to press the ENTER key:
Keybd_Event(VK_RETURN, 1, 0, 0); Keybd_Event(VK_RETURN, 1, KEYEVENTF_KEYUP, 0);
If several keys need being sent together, for example SHIFT + a: tell the system to press SHIFT, next press the 'a', next release 'a', next release SHIFT.