Saturday, December 19, 2009

How to send keyboard events in .NETCF

I've seen some people ask how to how to send keyboard events in .NET Compact Framework in the community. Although the operation is pretty simple, I'll share a code snippet anyway.

const int KEYEVENTF_KEYPRESS = 0x0000;
const int KEYEVENTF_KEYUP = 0x0002;
 
[DllImport("coredll.dll")]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
 
void SendKey(Keys key)
{
    keybd_event((byte) key, 0, KEYEVENTF_KEYPRESS, 0);
    keybd_event((byte) key, 0, KEYEVENTF_KEYUP, 0);
}

The code above will send the keyboard event to currently focused window or control

No comments: