在Windows Mobile 中模拟按键有时是非常有用的,比如我们可以发送Tab键使输入焦点自动跳转到下一个输入框。在Windows Form程序中,我们可以调用SystemCalls.SendKey()来实现,但不幸的是在.net framework ce中却没有SystemCalls这个类。在网上找到实现这个功能的代码,分享一下。
class SystemCalls
{
public const byte VK_NONAME = 0xFC; // 什么也不做
public const byte VK_ESC = 0x1B; // Smartphone的回退键
public const byte VK_F4 = 0x73; // Home Screen
public const byte VK_APP6 = 0xC6; // Smartphone上锁定键盘
public const byte VK_F22 = 0x85; // PocketPC上锁定键盘 (VK_KEYLOCK)
public const byte VK_F16 = 0x7F; // 出发扬声器
public const byte VK_OFF = 0x7F; //电源键
public const byte VK_TAB = 0x09; //Tab键
/// <summary>
/// 将按键送至全局键盘缓冲区
/// </summary>
/// <param name="key"></param>
public static void SendKey(byte key)
{
//const byte KEYEVENTF_SILENT = 0x0004;
const int KEYEVENTF_KEYUP = 0x02;
const int KEYEVENTF_KEYDOWN = 0x00;
keybd_event(key, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
}
[DllImport("coredll", SetLastError = true)]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
}
{
public const byte VK_NONAME = 0xFC; // 什么也不做
public const byte VK_ESC = 0x1B; // Smartphone的回退键
public const byte VK_F4 = 0x73; // Home Screen
public const byte VK_APP6 = 0xC6; // Smartphone上锁定键盘
public const byte VK_F22 = 0x85; // PocketPC上锁定键盘 (VK_KEYLOCK)
public const byte VK_F16 = 0x7F; // 出发扬声器
public const byte VK_OFF = 0x7F; //电源键
public const byte VK_TAB = 0x09; //Tab键
/// <summary>
/// 将按键送至全局键盘缓冲区
/// </summary>
/// <param name="key"></param>
public static void SendKey(byte key)
{
//const byte KEYEVENTF_SILENT = 0x0004;
const int KEYEVENTF_KEYUP = 0x02;
const int KEYEVENTF_KEYDOWN = 0x00;
keybd_event(key, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
}
[DllImport("coredll", SetLastError = true)]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
}