我使用 timer.tick
为 numpadkeys 创建数组并循环遍历它
GetAsyncKeyState(VK_LBUTTON)
否则
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646290(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646298(v=vs.85).aspx
How to Check if User input is from Barcode Scanner or Keyboard?
使用 RAW Input API 相对容易。
看看“区分条码扫描仪和 WinForms 中的键盘”
我有一个程序可以读取 3 个不同的 USB 扫描仪并将输入重定向到 3 个不同的“通道”进行处理。代码有点广泛,所以我不在这里发布它。如果您愿意,我可以粘贴一些内容或通过电子邮件将项目发送给您。
作为线索是进口:
#region Raw Input API
[DllImport( "User32.dll" )]
extern static uint GetRawInputDeviceList( IntPtr pRawInputDeviceList, ref uint uiNumDevices, uint cbSize );
[DllImport( "User32.dll" )]
extern static uint GetRawInputDeviceInfo( IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize );
[DllImport( "User32.dll" )]
extern static bool RegisterRawInputDevices( RAWINPUTDEVICE[ ] pRawInputDevice, uint uiNumDevices, uint cbSize );
[DllImport( "User32.dll" )]
extern static uint GetRawInputData( IntPtr hRawInput, uint uiCommand, IntPtr pData, ref uint pcbSize, uint cbSizeHeader );
#endregion
将 InputDevice 添加到项目后,您可以通过以下方式监听事件:
// 创建一个新的 InputDevice 对象并注册 InputDevice KeyPressed 事件处理程序。
input_dev = new InputDevice( Handle );
input_dev.KeyPressed += new InputDevice.DeviceEventHandler( m_KeyPressed );
The event handler m_KeyPressed lets you to distinguish your devices through e.Keyboard.SubClass
private void m_KeyPressed( object sender, InputDevice.KeyControlEventArgs e )
{
// e.Keyboard.SubClass tells you where from the event came.
// e.Keyboard.key gives you the input data.
}
希望有所帮助。