2015-10-30 52 views
1

我有兩個設備,條形碼掃描儀和鍵盤,通過USB連接到我的PC。我的目標是讀取Barcode-Scanner的所有輸入,並使用我的程序進行處理。此外,應該爲操作系統禁止掃描儀的輸入。處理和阻止來自特殊鍵盤的輸入

我找到了一個很好的文章,這個話題被稱爲: 結合原始輸入和鍵盤鉤子選擇性地從多個鍵盤

用這種方法我可以輸入(從哪個設備該輸入來阻斷輸入)爲我的程序,並阻止它的操作系統,就像我想。但是這種方法相當複雜,因爲它只適用於inputEvents和Hooks的組合。掛鉤在那裏阻塞操作系統的數據。 問題是,沒有辦法從Hook來自哪個設備。

我的問題是:有誰知道阻止輸入從一個特殊的鍵盤輸入不使用inputEvents和Hooks的組合?或者也許有可能告訴Hook來自哪個設備?

+0

是可能的附加信息附加到的窗口消息(見[SetMessageExtraInfo](HTTPS:/ /msdn.microsoft.com/en-us/library/windows/desktop/ms644954.aspx))。您是否檢查了條形碼掃描儀的文檔以查看是否可以提供其他信息? – IInspectable

回答

-2

我用timer.tick 通過

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?

是比較容易與原始輸入API來完成創建numpadkeys和循環數組。

看看「從鍵盤上區分條碼掃描儀的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. 
    } 

希望能有所幫助。

+0

感謝您的幫助,但恐怕您的回答對我的理解有些短暫。你到底在做什麼? – Andre

+0

我爲GS1一個SSCC碼條形碼數據庫程序但in.net –

+0

'GetAsyncKeyState'不能在不同的輸入設備之間進行區分。而且在定時器中使用時不可靠。由於它基於快照,因此可能會錯過鍵盤輸入,這發生在連續採樣點之間。發送鍵盤輸入的設備通常比用戶更快。 – IInspectable

-3

如果您閱讀我的第一個鏈接。

typedef struct tagINPUT_MESSAGE_SOURCE { 
    INPUT_MESSAGE_DEVICE_TYPE deviceType; 
    INPUT_MESSAGE_ORIGIN_ID originId; 
} INPUT_MESSAGE_SOURCE; 

https://msdn.microsoft.com/en-us/library/windows/desktop/hh448799(v=vs.85).aspx POS for .NET | Differentiate between (barcode) scanner and keyboard input 和從掃描器接合數據可以與vbcrlf拆分

http://msgroups.net/development.device.drivers/how-to-uniquely-identify-a-usb-hid/14444

+0

這不能區分兩個不同的USB設備,它們將自己註冊爲系統的鍵盤。該結構將爲兩個設備保存「IMDT_KEYBOARD」和「IMO_HARDWARE」。而且,請不要在你想添加任何東西的時候開始一個新的答案。使用上一個答案中的[編輯](http://stackoverflow.com/posts/33430967/edit)鏈接,並更新該鏈接。 – IInspectable

+0

https://bytes.com/topic/c-sharp/answers/262261-identifying-input-keyboard-devices –

+0

[我怎樣寫一個很好的答案?](http://stackoverflow.com/help/how-回答)。這是強制性閱讀。我曾多次指出,你忽略了被問到的問題。你提出的解決方案都沒有認真地試圖解決這個問題。 – IInspectable