在使用 using 等新語法時,在 VisualStudio 2019 會自動判斷框架版本,如在 net 45 就不會自動使用最新版本的語法,需要修改項目文件 在使用 C# 8.0 之前,請在官網 下載最新的 VisualStudio 2019 版本 如果在編譯時提示 “Using 聲明”在 C# ...
項目需要在觸摸屏上增加一個虛擬鍵盤。記錄下過程中遇到的問題及解決方法。
1.模擬按鍵
網上找到如下3種方法
1)SendKeys.Send
測試單獨的shift不好用,所以最終未採納此方法
SendKeys.Send("^{E}");//shift+e SendKeys.Send("{Enter}");
2)keybd_event
最終選擇了這個方法,簡單有效。
虛擬按鍵對照表:https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)] public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo); [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
public static uint KEYEVENTF_KEYDOWN = 0;// 鍵按下 KEYEVENTF_KEYDOWN = 0
public static uint KEYEVENTF_KEYUP = 2;//鍵彈起 KEYEVENTF_KEYUP = 2
keybd_event(0x14, 0, KEYEVENTF_KEYDOWN, 0); //鍵按下 KEYEVENTF_KEYDOWN = 0
keybd_event(0x14, 0, KEYEVENTF_KEYUP, 0); //鍵彈起 KEYEVENTF_KEYUP = 2
3)PostMessage
這個方法也是簡單有效,但是據說有些其他問題,沒有仔細研究,有興趣的可以自己研究。
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] public static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam); PostMessage(textBox2.Handle, 256, Keys.D, 2);//模擬按下2次 Keys.D
2.各種類型鍵的處理
微軟對鍵盤輸入有進行了分類,可以參考這個。
https://learn.microsoft.com/zh-cn/windows/win32/learnwin32/keyboard-input
1)CapsLock鍵/NumLock鍵
想要實現鍵盤按下抬起與界面效果相同,就需要捕獲鍵盤操作。我使用了Hook
以下這篇博客寫的很清晰明瞭,就不贅述了。
https://www.cnblogs.com/chorm590/p/14199978.html
在hook回調函數中做瞭如下處理
private int keyboardHookCallback(int code, IntPtr wParam, IntPtr lParam) { if (code < 0) { return User32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam); } else { Keyboard_LL_Hook_Data khd = (Keyboard_LL_Hook_Data)Marshal.PtrToStructure(lParam, typeof(Keyboard_LL_Hook_Data)); System.Diagnostics.Debug.WriteLine($"key event:{wParam}, key code:{khd.vkCode}, event time:{khd.time}"); if ((int)wParam == 256 && khd.vkCode == 0x14) { IsCapsLockDown = !IsCapsLockDown; } return User32.CallNextHookEx(IntPtr.Zero, code, wParam, lParam); } } private bool isCapsLockDown; private bool IsCapsLockDown { get { return isCapsLockDown; } set { isCapsLockDown = value; this.buttonCapsLock.BackColor = value ? Color.Gray : Control.DefaultBackColor; } } private void buttonCapsLock_Click(object sender, EventArgs e) { User32.keybd_event(VirtualKeyCode.CAPS_LOCK, 0, 0, 0); //鍵按下 KEYEVENTF_KEYDOWN = 0 User32.keybd_event(VirtualKeyCode.CAPS_LOCK, 0, 2, 0); //鍵彈起 KEYEVENTF_KEYUP = 2 }
初始化時同步鍵盤CapsLock/NumLock狀態,使用user32.GetKeyState.返回值等於1則按下狀態
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern short GetKeyState(int keyCode); public static bool GetState(byte VKeyCode) { return (User32.GetKeyState(VKeyCode) == 1); }
2)其他組合鍵處理
ctrl+shift切換輸入法,只響應這一種功能鍵+功能鍵。其他小伙伴有好的方法可以分享一下。
3)輸入鍵+組合鍵
如ctrl+c,ctrl+v,ctrl+a,ctrl+z等等。
此方法不是最優方式,可以改進功能鍵存放在Queue中,遍歷功能鍵,按順序響應。
private void HandleKeysCombin(byte VKCode) { //設置焦點控制項 this.ActiveControl = this.m_CustomActiveControl; lock (this) { if (IsCtrlDown) { keybd_event(VirtualKeyCode.CTRL, 0, KEYEVENTF_KEYDOWN, 0); //鍵按下 KEYEVENTF_KEYDOWN = 0 } if (IsShiftDown) { keybd_event(VirtualKeyCode.SHIFT, 0, KEYEVENTF_KEYDOWN, 0); //鍵按下 KEYEVENTF_KEYDOWN = 0 } if (IsAltDown) { keybd_event(VirtualKeyCode.ALT, 0, KEYEVENTF_KEYDOWN, 0); //鍵按下 KEYEVENTF_KEYDOWN = 0 } keybd_event(VKCode, VKCode, User32.KEYEVENTF_KEYDOWN, 0); //鍵按下 KEYEVENTF_KEYDOWN = 0 keybd_event(VKCode, VKCode, User32.KEYEVENTF_KEYUP, 0); //鍵彈起 KEYEVENTF_KEYUP = 2 if (IsCtrlDown) { keybd_event(VirtualKeyCode.CTRL, 0, KEYEVENTF_KEYUP, 0); //鍵彈起 KEYEVENTF_KEYUP = 2 IsCtrlDown = false; } if (IsShiftDown) { keybd_event(VirtualKeyCode.SHIFT, 0, KEYEVENTF_KEYUP, 0); //鍵彈起 KEYEVENTF_KEYUP = 2 IsShiftDown = false; } if (IsAltDown) { keybd_event(VirtualKeyCode.ALT, 0, KEYEVENTF_KEYUP, 0); //鍵彈起 KEYEVENTF_KEYUP = 2 IsAltDown = false; } } }
3.窗體/UserControl焦點的處理
這篇裡面方法簡單好用,不贅述
https://blog.csdn.net/accomp/article/details/7209052