使用RAW Input API比較容易。
看看「Distinguishing Barcode Scanners from the Keyboard in 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
到項目後,您可以通過監聽事件:
// Create a new InputDevice object and register InputDevice KeyPressed event handler.
input_dev = new InputDevice(Handle);
input_dev.KeyPressed += new InputDevice.DeviceEventHandler(m_KeyPressed);
事件處理m_KeyPressed
可以讓你分辨你設備通過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.
}
希望能有所幫助。
來源
2011-02-28 17:21:34
j4x
什麼問題?如何檢測來自鍵盤的輸入是否會幫助您?你只是要扔掉任何鍵盤輸入?如果條碼掃描器損壞或客戶卡上的條形碼不可讀?如果他們可以用鍵盤手動輸入數字不是更好嗎?這個問題不是軟件相關的;這是人類的情況。教員工不要使用鍵盤,除非發生緊急情況。 – 2011-02-23 08:10:06
是的你的權利..我正在想其他的方式來使應用程序更安全... – Crimsonland 2011-02-23 08:22:39
如果你想讓它變得不可能,我不知道隱藏文本框有什麼問題。他們無法輸入隱藏文本框。如果你不想讓它變得不可能,我不確定你會爲安全做些什麼。我無法想象這是一個真正的安全問題。如果你真的認爲員工會偷吃對方的餐券,請出示圖片確認。 (我不明白你對你提出的問題所作的編輯*) – 2011-02-23 08:31:53