Mehrere unabhängige Tastaturen an einem PC

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Gerd_F.

    Mehrere unabhängige Tastaturen an einem PC

    Hallo,
    ich bin ganz neu hier und habe anscheinend genau die Lösung für mein Problem gefunden. Allerdings weiß ich nicht, wie ich das von Myrax angegebene Projekt unter VB2010 zum Laufen bekommen kann.

    Ich brauche 3 Nummernpads und eine Tastatur an einem PC - alle unabhängig voneinander. Kann mir jemand weiterhelfen?

    Ausgelagert aus Mehrere USB-Tastaturen getrennt abfragen ~Thunderbolt

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Thunderbolt“ ()

    Hallo,
    Meine Frage lautete: Wie kann ich das von Myrax angegebene Projekt "RawInput" codeproject.com/Articles/17123…-handle-multiple-keyboard unter VB2010 zum Laufen bringen? Mit diesem Projekt scheint es möglich zu sei, mehrere Tastaturen unabhängig voneinander abzufragen.

    Ich habe die Dateien aus dem angegebenen Link heruntergeladen, nach VB konvertiert und da der Konverter VB2013 ausgibt, nach vb2010 konvertiert.
    Nun bekomme ich lauter Fehlermeldungen, dass in Variablen zu viele Zeichen sind, allerdings sind die Zeilen, für die der Fehler gemeldet wird, Kommentarzeilen.

    Falls es auch einfacher gehen sollte: Ich habe 3 Nummernpads und eine Tastatur, die an einem PC unabhängig voneinander abgefragt werden müssen. Leider bin ich nicht so versiert, so etwas elbst zu programmieren. Würde das jemand (auch gegen Bezahlung) übernehmen?

    Gerd_F schrieb:

    ... , nach VB konvertiert und da der Konverter VB2013 ausgibt, nach vb2010 konvertiert.

    Du verwechselst da IDE und Sprache ! Die Sprache, die du meinst, ist Visual Basic .Net.
    Mit VB2010 ist die IDE gemeint !

    Ich habe den code mal durch einen Converter gejagt, bekomme den letzten Fehler aber nicht behoben, RaiseEvent ist mir völlig unbekannt und was ich so gefunden habe, hat mir nicht weiter geholfen, vieleicht kann ja mal ein "Pro" aushelfen.

    Fehlermeldung schrieb:

    Fehler 2 "Public Event KeyPressed(sender As Object, e As KeyControlEventArgs)" ist ein Ereignis und kann nicht direkt aufgerufen werden. Verwenden Sie eine RaiseEvent-Anweisung, um ein Ereignis aufzurufen. ...
    InputDevice.vb Zeile 459



    Code
    InputDevice.vb

    Quellcode

    1. Imports System.Collections
    2. Imports System.IO
    3. Imports System.Runtime.InteropServices
    4. Imports System.Windows.Forms
    5. Imports Microsoft.Win32
    6. Namespace RawInput
    7. ''' <summary>
    8. ''' Handles raw input from keyboard devices.
    9. ''' </summary>
    10. Public NotInheritable Class InputDevice
    11. #Region "const definitions"
    12. ' The following constants are defined in Windows.h
    13. Private Const RIDEV_INPUTSINK As Integer = &H100
    14. Private Const RID_INPUT As Integer = &H10000003
    15. Private Const FAPPCOMMAND_MASK As Integer = &HF000
    16. Private Const FAPPCOMMAND_MOUSE As Integer = &H8000
    17. Private Const FAPPCOMMAND_OEM As Integer = &H1000
    18. Private Const RIM_TYPEMOUSE As Integer = 0
    19. Private Const RIM_TYPEKEYBOARD As Integer = 1
    20. Private Const RIM_TYPEHID As Integer = 2
    21. Private Const RIDI_DEVICENAME As Integer = &H20000007
    22. Private Const WM_KEYDOWN As Integer = &H100
    23. Private Const WM_SYSKEYDOWN As Integer = &H104
    24. Private Const WM_INPUT As Integer = &HFF
    25. Private Const VK_OEM_CLEAR As Integer = &HFE
    26. Private Const VK_LAST_KEY As Integer = VK_OEM_CLEAR
    27. ' this is a made up value used as a sentinel
    28. #End Region
    29. #Region "structs & enums"
    30. ''' <summary>
    31. ''' An enum representing the different types of input devices.
    32. ''' </summary>
    33. Public Enum DeviceType
    34. Key
    35. Mouse
    36. OEM
    37. End Enum
    38. ''' <summary>
    39. ''' Class encapsulating the information about a
    40. ''' keyboard event, including the device it
    41. ''' originated with and what key was pressed
    42. ''' </summary>
    43. Public Class DeviceInfo
    44. Public deviceName As String
    45. Public deviceType As String
    46. Public deviceHandle As IntPtr
    47. Public Name As String
    48. Public source As String
    49. Public key As UShort
    50. Public vKey As String
    51. End Class
    52. #Region "Windows.h structure declarations"
    53. ' The following structures are defined in Windows.h
    54. <StructLayout(LayoutKind.Sequential)> _
    55. Friend Structure RAWINPUTDEVICELIST
    56. Public hDevice As IntPtr
    57. <MarshalAs(UnmanagedType.U4)> _
    58. Public dwType As Integer
    59. End Structure
    60. <StructLayout(LayoutKind.Explicit)> _
    61. Friend Structure RAWINPUT
    62. <FieldOffset(0)> _
    63. Public header As RAWINPUTHEADER
    64. <FieldOffset(16)> _
    65. Public mouse As RAWMOUSE
    66. <FieldOffset(16)> _
    67. Public keyboard As RAWKEYBOARD
    68. <FieldOffset(16)> _
    69. Public hid As RAWHID
    70. End Structure
    71. <StructLayout(LayoutKind.Sequential)> _
    72. Friend Structure RAWINPUTHEADER
    73. <MarshalAs(UnmanagedType.U4)> _
    74. Public dwType As Integer
    75. <MarshalAs(UnmanagedType.U4)> _
    76. Public dwSize As Integer
    77. Public hDevice As IntPtr
    78. <MarshalAs(UnmanagedType.U4)> _
    79. Public wParam As Integer
    80. End Structure
    81. <StructLayout(LayoutKind.Sequential)> _
    82. Friend Structure RAWHID
    83. <MarshalAs(UnmanagedType.U4)> _
    84. Public dwSizHid As Integer
    85. <MarshalAs(UnmanagedType.U4)> _
    86. Public dwCount As Integer
    87. End Structure
    88. <StructLayout(LayoutKind.Sequential)> _
    89. Friend Structure BUTTONSSTR
    90. <MarshalAs(UnmanagedType.U2)> _
    91. Public usButtonFlags As UShort
    92. <MarshalAs(UnmanagedType.U2)> _
    93. Public usButtonData As UShort
    94. End Structure
    95. <StructLayout(LayoutKind.Explicit)> _
    96. Friend Structure RAWMOUSE
    97. <MarshalAs(UnmanagedType.U2)> _
    98. <FieldOffset(0)> _
    99. Public usFlags As UShort
    100. <MarshalAs(UnmanagedType.U4)> _
    101. <FieldOffset(4)> _
    102. Public ulButtons As UInteger
    103. <FieldOffset(4)> _
    104. Public buttonsStr As BUTTONSSTR
    105. <MarshalAs(UnmanagedType.U4)> _
    106. <FieldOffset(8)> _
    107. Public ulRawButtons As UInteger
    108. <FieldOffset(12)> _
    109. Public lLastX As Integer
    110. <FieldOffset(16)> _
    111. Public lLastY As Integer
    112. <MarshalAs(UnmanagedType.U4)> _
    113. <FieldOffset(20)> _
    114. Public ulExtraInformation As UInteger
    115. End Structure
    116. <StructLayout(LayoutKind.Sequential)> _
    117. Friend Structure RAWKEYBOARD
    118. <MarshalAs(UnmanagedType.U2)> _
    119. Public MakeCode As UShort
    120. <MarshalAs(UnmanagedType.U2)> _
    121. Public Flags As UShort
    122. <MarshalAs(UnmanagedType.U2)> _
    123. Public Reserved As UShort
    124. <MarshalAs(UnmanagedType.U2)> _
    125. Public VKey As UShort
    126. <MarshalAs(UnmanagedType.U4)> _
    127. Public Message As UInteger
    128. <MarshalAs(UnmanagedType.U4)> _
    129. Public ExtraInformation As UInteger
    130. End Structure
    131. <StructLayout(LayoutKind.Sequential)> _
    132. Friend Structure RAWINPUTDEVICE
    133. <MarshalAs(UnmanagedType.U2)> _
    134. Public usUsagePage As UShort
    135. <MarshalAs(UnmanagedType.U2)> _
    136. Public usUsage As UShort
    137. <MarshalAs(UnmanagedType.U4)> _
    138. Public dwFlags As Integer
    139. Public hwndTarget As IntPtr
    140. End Structure
    141. #End Region
    142. #End Region
    143. #Region "DllImports"
    144. <DllImport("User32.dll")> _
    145. Private Shared Function GetRawInputDeviceList(pRawInputDeviceList As IntPtr, ByRef uiNumDevices As UInteger, cbSize As UInteger) As UInteger
    146. End Function
    147. <DllImport("User32.dll")> _
    148. Private Shared Function GetRawInputDeviceInfo(hDevice As IntPtr, uiCommand As UInteger, pData As IntPtr, ByRef pcbSize As UInteger) As UInteger
    149. End Function
    150. <DllImport("User32.dll")> _
    151. Private Shared Function RegisterRawInputDevices(pRawInputDevice As RAWINPUTDEVICE(), uiNumDevices As UInteger, cbSize As UInteger) As Boolean
    152. End Function
    153. <DllImport("User32.dll")> _
    154. Private Shared Function GetRawInputData(hRawInput As IntPtr, uiCommand As UInteger, pData As IntPtr, ByRef pcbSize As UInteger, cbSizeHeader As UInteger) As UInteger
    155. End Function
    156. #End Region
    157. #Region "Variables and event handling"
    158. ''' <summary>
    159. ''' List of keyboard devices. Key: the device handle
    160. ''' Value: the device info class
    161. ''' </summary>
    162. Private deviceList As New Hashtable()
    163. ''' <summary>
    164. ''' The delegate to handle KeyPressed events.
    165. ''' </summary>
    166. ''' <param name="sender">The object sending the event.</param>
    167. ''' <param name="e">A set of KeyControlEventArgs information about the key that was pressed and the device it was on.</param>
    168. Public Delegate Sub DeviceEventHandler(sender As Object, e As KeyControlEventArgs)
    169. ''' <summary>
    170. ''' The event raised when InputDevice detects that a key was pressed.
    171. ''' </summary>
    172. Public Event KeyPressed As DeviceEventHandler
    173. ''' <summary>
    174. ''' Arguments provided by the handler for the KeyPressed
    175. ''' event.
    176. ''' </summary>
    177. Public Class KeyControlEventArgs
    178. Inherits EventArgs
    179. Private m_deviceInfo As DeviceInfo
    180. Private m_device As DeviceType
    181. Public Sub New(dInfo As DeviceInfo, device As DeviceType)
    182. m_deviceInfo = dInfo
    183. m_device = device
    184. End Sub
    185. Public Sub New()
    186. End Sub
    187. Public Property Keyboard() As DeviceInfo
    188. Get
    189. Return m_deviceInfo
    190. End Get
    191. Set(value As DeviceInfo)
    192. m_deviceInfo = value
    193. End Set
    194. End Property
    195. Public Property Device() As DeviceType
    196. Get
    197. Return m_device
    198. End Get
    199. Set(value As DeviceType)
    200. m_device = value
    201. End Set
    202. End Property
    203. End Class
    204. #End Region
    205. #Region "InputDevice( IntPtr hwnd )"
    206. ''' <summary>
    207. ''' InputDevice constructor; registers the raw input devices
    208. ''' for the calling window.
    209. ''' </summary>
    210. ''' <param name="hwnd">Handle of the window listening for key presses</param>
    211. Public Sub New(hwnd As IntPtr)
    212. 'Create an array of all the raw input devices we want to
    213. 'listen to. In this case, only keyboard devices.
    214. 'RIDEV_INPUTSINK determines that the window will continue
    215. 'to receive messages even when it doesn't have the focus.
    216. Dim rid As RAWINPUTDEVICE() = New RAWINPUTDEVICE(0) {}
    217. rid(0).usUsagePage = &H1
    218. rid(0).usUsage = &H6
    219. rid(0).dwFlags = RIDEV_INPUTSINK
    220. rid(0).hwndTarget = hwnd
    221. If Not RegisterRawInputDevices(rid, CUInt(rid.Length), CUInt(Marshal.SizeOf(rid(0)))) Then
    222. Throw New ApplicationException("Failed to register raw input device(s).")
    223. End If
    224. End Sub
    225. #End Region
    226. #Region "ReadReg( string item, ref bool isKeyboard )"
    227. ''' <summary>
    228. ''' Reads the Registry to retrieve a friendly description
    229. ''' of the device, and determine whether it is a keyboard.
    230. ''' </summary>
    231. ''' <param name="item">The device name to search for, as provided by GetRawInputDeviceInfo.</param>
    232. ''' <param name="isKeyboard">Determines whether the device's class is "Keyboard".</param>
    233. ''' <returns>The device description stored in the Registry entry's DeviceDesc value.</returns>
    234. Private Function ReadReg(item As String, ByRef isKeyboard As Boolean) As String
    235. ' Example Device Identification string
    236. ' @"\??\ACPI#PNP0303#3&13c0b0c5&0#{884b96c3-56ef-11d1-bc8c-00a0c91405dd}";
    237. ' remove the \??\
    238. item = item.Substring(4)
    239. Dim split As String() = item.Split("#"c)
    240. Dim id_01 As String = split(0)
    241. ' ACPI (Class code)
    242. Dim id_02 As String = split(1)
    243. ' PNP0303 (SubClass code)
    244. Dim id_03 As String = split(2)
    245. ' 3&13c0b0c5&0 (Protocol code)
    246. 'The final part is the class GUID and is not needed here
    247. 'Open the appropriate key as read-only so no permissions
    248. 'are needed.
    249. Dim OurKey As RegistryKey = Registry.LocalMachine
    250. Dim findme As String = String.Format("System\CurrentControlSet\Enum\{0}\{1}\{2}", id_01, id_02, id_03)
    251. OurKey = OurKey.OpenSubKey(findme, False)
    252. 'Retrieve the desired information and set isKeyboard
    253. Dim deviceDesc As String = DirectCast(OurKey.GetValue("DeviceDesc"), String)
    254. Dim deviceClass As String = DirectCast(OurKey.GetValue("Class"), String)
    255. If deviceClass.ToUpper().Equals("KEYBOARD") Then
    256. isKeyboard = True
    257. Else
    258. isKeyboard = False
    259. End If
    260. Return deviceDesc
    261. End Function
    262. #End Region
    263. #Region "int EnumerateDevices()"
    264. ''' <summary>
    265. ''' Iterates through the list provided by GetRawInputDeviceList,
    266. ''' counting keyboard devices and adding them to deviceList.
    267. ''' </summary>
    268. ''' <returns>The number of keyboard devices found.</returns>
    269. Public Function EnumerateDevices() As Integer
    270. Dim NumberOfDevices As Integer = 0
    271. Dim deviceCount As UInteger = 0
    272. Dim dwSize As Integer = (Marshal.SizeOf(GetType(RAWINPUTDEVICELIST)))
    273. ' Get the number of raw input devices in the list,
    274. ' then allocate sufficient memory and get the entire list
    275. If GetRawInputDeviceList(IntPtr.Zero, deviceCount, CUInt(dwSize)) = 0 Then
    276. Dim pRawInputDeviceList As IntPtr = Marshal.AllocHGlobal(CInt(dwSize * deviceCount))
    277. GetRawInputDeviceList(pRawInputDeviceList, deviceCount, CUInt(dwSize))
    278. ' Iterate through the list, discarding undesired items
    279. ' and retrieving further information on keyboard devices
    280. For i As Integer = 0 To CInt(deviceCount - 1)
    281. Dim dInfo As DeviceInfo
    282. Dim deviceName As String
    283. Dim pcbSize As UInteger = 0
    284. Dim rid As RAWINPUTDEVICELIST = CType(Marshal.PtrToStructure(New IntPtr((pRawInputDeviceList.ToInt32() + (dwSize * i))), GetType(RAWINPUTDEVICELIST)), RAWINPUTDEVICELIST)
    285. GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, IntPtr.Zero, pcbSize)
    286. If pcbSize > 0 Then
    287. Dim pData As IntPtr = Marshal.AllocHGlobal(CInt(pcbSize))
    288. GetRawInputDeviceInfo(rid.hDevice, RIDI_DEVICENAME, pData, pcbSize)
    289. deviceName = DirectCast(Marshal.PtrToStringAnsi(pData), String)
    290. ' Drop the "root" keyboard and mouse devices used for Terminal
    291. ' Services and the Remote Desktop
    292. If deviceName.ToUpper().Contains("ROOT") Then
    293. Continue For
    294. End If
    295. ' If the device is identified in the list as a keyboard or
    296. ' HID device, create a DeviceInfo object to store information
    297. ' about it
    298. If rid.dwType = RIM_TYPEKEYBOARD OrElse rid.dwType = RIM_TYPEHID Then
    299. dInfo = New DeviceInfo()
    300. dInfo.deviceName = DirectCast(Marshal.PtrToStringAnsi(pData), String)
    301. dInfo.deviceHandle = rid.hDevice
    302. dInfo.deviceType = GetDeviceType(rid.dwType)
    303. ' Check the Registry to see whether this is actually a
    304. ' keyboard, and to retrieve a more friendly description.
    305. Dim IsKeyboardDevice As Boolean = False
    306. Dim DeviceDesc As String = ReadReg(deviceName, IsKeyboardDevice)
    307. dInfo.Name = DeviceDesc
    308. ' If it is a keyboard and it isn't already in the list,
    309. ' add it to the deviceList hashtable and increase the
    310. ' NumberOfDevices count
    311. If Not deviceList.Contains(rid.hDevice) AndAlso IsKeyboardDevice Then
    312. NumberOfDevices += 1
    313. deviceList.Add(rid.hDevice, dInfo)
    314. End If
    315. End If
    316. Marshal.FreeHGlobal(pData)
    317. End If
    318. Next
    319. Marshal.FreeHGlobal(pRawInputDeviceList)
    320. Return NumberOfDevices
    321. Else
    322. Throw New ApplicationException("An error occurred while retrieving the list of devices.")
    323. End If
    324. End Function
    325. #End Region
    326. #Region "ProcessInputCommand( Message message )"
    327. ''' <summary>
    328. ''' Processes WM_INPUT messages to retrieve information about any
    329. ''' keyboard events that occur.
    330. ''' </summary>
    331. ''' <param name="message">The WM_INPUT message to process.</param>
    332. Public Sub ProcessInputCommand(message As Message)
    333. Dim dwSize As UInteger = 0
    334. ' First call to GetRawInputData sets the value of dwSize,
    335. ' which can then be used to allocate the appropriate amount of memory,
    336. ' storing the pointer in "buffer".
    337. GetRawInputData(message.LParam, RID_INPUT, IntPtr.Zero, dwSize, CUInt(Marshal.SizeOf(GetType(RAWINPUTHEADER))))
    338. Dim buffer As IntPtr = Marshal.AllocHGlobal(CInt(dwSize))
    339. Try
    340. ' Check that buffer points to something, and if so,
    341. ' call GetRawInputData again to fill the allocated memory
    342. ' with information about the input
    343. If buffer <> IntPtr.Zero AndAlso GetRawInputData(message.LParam, RID_INPUT, buffer, dwSize, CUInt(Marshal.SizeOf(GetType(RAWINPUTHEADER)))) = dwSize Then
    344. ' Store the message information in "raw", then check
    345. ' that the input comes from a keyboard device before
    346. ' processing it to raise an appropriate KeyPressed event.
    347. Dim raw As RAWINPUT = CType(Marshal.PtrToStructure(buffer, GetType(RAWINPUT)), RAWINPUT)
    348. If raw.header.dwType = RIM_TYPEKEYBOARD Then
    349. ' Filter for Key Down events and then retrieve information
    350. ' about the keystroke
    351. If raw.keyboard.Message = WM_KEYDOWN OrElse raw.keyboard.Message = WM_SYSKEYDOWN Then
    352. Dim key As UShort = raw.keyboard.VKey
    353. ' On most keyboards, "extended" keys such as the arrow or
    354. ' page keys return two codes - the key's own code, and an
    355. ' "extended key" flag, which translates to 255. This flag
    356. ' isn't useful to us, so it can be disregarded.
    357. If key > VK_LAST_KEY Then
    358. Return
    359. End If
    360. ' Retrieve information about the device and the
    361. ' key that was pressed.
    362. Dim dInfo As DeviceInfo = Nothing
    363. If deviceList.Contains(raw.header.hDevice) Then
    364. Dim myKey As Keys
    365. dInfo = DirectCast(deviceList(raw.header.hDevice), DeviceInfo)
    366. myKey = CType([Enum].Parse(GetType(Keys), [Enum].GetName(GetType(Keys), key)), Keys)
    367. dInfo.vKey = myKey.ToString()
    368. dInfo.key = key
    369. Else
    370. Dim errMessage As String = [String].Format("Handle :{0} was not in hashtable. The device may support more than one handle or usage page, and is probably not a standard keyboard.", raw.header.hDevice)
    371. Throw New ApplicationException(errMessage)
    372. End If
    373. ' If the key that was pressed is valid and there
    374. ' was no problem retrieving information on the device,
    375. ' raise the KeyPressed event.
    376. If KeyPressed IsNot Nothing AndAlso dInfo IsNot Nothing Then
    377. RaiseEvent KeyPressed(Me, New KeyControlEventArgs(dInfo, GetDevice(message.LParam.ToInt32())))
    378. Else
    379. Dim errMessage As String = [String].Format("Received Unknown Key: {0}. Possibly an unknown device", key)
    380. Throw New ApplicationException(errMessage)
    381. End If
    382. End If
    383. End If
    384. End If
    385. Finally
    386. Marshal.FreeHGlobal(buffer)
    387. End Try
    388. End Sub
    389. #End Region
    390. #Region "DeviceType GetDevice( int param )"
    391. ''' <summary>
    392. ''' Determines what type of device triggered a WM_INPUT message.
    393. ''' (Used in the ProcessInputCommand method).
    394. ''' </summary>
    395. ''' <param name="param">The LParam from a WM_INPUT message.</param>
    396. ''' <returns>A DeviceType enum value.</returns>
    397. Private Function GetDevice(param As Integer) As DeviceType
    398. Dim deviceType__1 As DeviceType
    399. Select Case CInt(CUShort(param >> 16) And FAPPCOMMAND_MASK)
    400. Case FAPPCOMMAND_OEM
    401. deviceType__1 = DeviceType.OEM
    402. Exit Select
    403. Case FAPPCOMMAND_MOUSE
    404. deviceType__1 = DeviceType.Mouse
    405. Exit Select
    406. Case Else
    407. deviceType__1 = DeviceType.Key
    408. Exit Select
    409. End Select
    410. Return deviceType__1
    411. End Function
    412. #End Region
    413. #Region "ProcessMessage( Message message )"
    414. ''' <summary>
    415. ''' Filters Windows messages for WM_INPUT messages and calls
    416. ''' ProcessInputCommand if necessary.
    417. ''' </summary>
    418. ''' <param name="message">The Windows message.</param>
    419. Public Sub ProcessMessage(message As Message)
    420. Select Case message.Msg
    421. Case WM_INPUT
    422. If True Then
    423. ProcessInputCommand(message)
    424. End If
    425. Exit Select
    426. End Select
    427. End Sub
    428. #End Region
    429. #Region "GetDeviceType( int device )"
    430. ''' <summary>
    431. ''' Converts a RAWINPUTDEVICELIST dwType value to a string
    432. ''' describing the device type.
    433. ''' </summary>
    434. ''' <param name="device">A dwType value (RIM_TYPEMOUSE,
    435. ''' RIM_TYPEKEYBOARD or RIM_TYPEHID).</param>
    436. ''' <returns>A string representation of the input value.</returns>
    437. Private Function GetDeviceType(device As Integer) As String
    438. Dim deviceType As String
    439. Select Case device
    440. Case RIM_TYPEMOUSE
    441. deviceType = "MOUSE"
    442. Exit Select
    443. Case RIM_TYPEKEYBOARD
    444. deviceType = "KEYBOARD"
    445. Exit Select
    446. Case RIM_TYPEHID
    447. deviceType = "HID"
    448. Exit Select
    449. Case Else
    450. deviceType = "UNKNOWN"
    451. Exit Select
    452. End Select
    453. Return deviceType
    454. End Function
    455. #End Region
    456. End Class
    457. End Namespace


    Form1.vb

    VB.NET-Quellcode

    1. Imports System.Windows.Forms
    2. Namespace RawInput
    3. Partial Public Class Form1
    4. Inherits Form
    5. Private id As InputDevice
    6. Private NumberOfKeyboards As Integer
    7. Public Sub New()
    8. InitializeComponent()
    9. ' Create a new InputDevice object, get the number of
    10. ' keyboards, and register the method which will handle the
    11. ' InputDevice KeyPressed event
    12. id = New InputDevice(Handle)
    13. NumberOfKeyboards = id.EnumerateDevices()
    14. id.KeyPressed += New InputDevice.DeviceEventHandler(AddressOf m_KeyPressed)
    15. End Sub
    16. ' The WndProc is overridden to allow InputDevice to intercept
    17. ' messages to the window and thus catch WM_INPUT messages
    18. Protected Overrides Sub WndProc(ByRef message As Message)
    19. If id IsNot Nothing Then
    20. id.ProcessMessage(message)
    21. End If
    22. MyBase.WndProc(message)
    23. End Sub
    24. Private Sub m_KeyPressed(sender As Object, e As InputDevice.KeyControlEventArgs)
    25. 'Replace() is just a cosmetic fix to stop ampersands turning into underlines
    26. lbHandle.Text = e.Keyboard.deviceHandle.ToString()
    27. lbType.Text = e.Keyboard.deviceType
    28. lbName.Text = e.Keyboard.deviceName.Replace("&", "&&")
    29. lbDescription.Text = e.Keyboard.Name
    30. lbKey.Text = e.Keyboard.key.ToString()
    31. lbNumKeyboards.Text = NumberOfKeyboards.ToString()
    32. lbVKey.Text = e.Keyboard.vKey
    33. End Sub
    34. Private Sub btnClose_Click(sender As Object, e As System.EventArgs)
    35. Me.Close()
    36. End Sub
    37. Friend WithEvents lbHandle As System.Windows.Forms.Label
    38. Private Sub InitializeComponent()
    39. Me.lbHandle = New System.Windows.Forms.Label()
    40. Me.lbType = New System.Windows.Forms.Label()
    41. Me.lbName = New System.Windows.Forms.Label()
    42. Me.lbDescription = New System.Windows.Forms.Label()
    43. Me.lbKey = New System.Windows.Forms.Label()
    44. Me.lbNumKeyboards = New System.Windows.Forms.Label()
    45. Me.lbVKey = New System.Windows.Forms.Label()
    46. Me.SuspendLayout()
    47. '
    48. 'lbHandle
    49. '
    50. Me.lbHandle.AutoSize = True
    51. Me.lbHandle.Location = New System.Drawing.Point(13, 13)
    52. Me.lbHandle.Name = "lbHandle"
    53. Me.lbHandle.Size = New System.Drawing.Size(39, 13)
    54. Me.lbHandle.TabIndex = 0
    55. Me.lbHandle.Text = "Label1"
    56. '
    57. 'lbType
    58. '
    59. Me.lbType.AutoSize = True
    60. Me.lbType.Location = New System.Drawing.Point(13, 42)
    61. Me.lbType.Name = "lbType"
    62. Me.lbType.Size = New System.Drawing.Size(39, 13)
    63. Me.lbType.TabIndex = 1
    64. Me.lbType.Text = "Label2"
    65. '
    66. 'lbName
    67. '
    68. Me.lbName.AutoSize = True
    69. Me.lbName.Location = New System.Drawing.Point(12, 72)
    70. Me.lbName.Name = "lbName"
    71. Me.lbName.Size = New System.Drawing.Size(39, 13)
    72. Me.lbName.TabIndex = 2
    73. Me.lbName.Text = "Label3"
    74. '
    75. 'lbDescription
    76. '
    77. Me.lbDescription.AutoSize = True
    78. Me.lbDescription.Location = New System.Drawing.Point(13, 98)
    79. Me.lbDescription.Name = "lbDescription"
    80. Me.lbDescription.Size = New System.Drawing.Size(39, 13)
    81. Me.lbDescription.TabIndex = 3
    82. Me.lbDescription.Text = "Label4"
    83. '
    84. 'lbKey
    85. '
    86. Me.lbKey.AutoSize = True
    87. Me.lbKey.Location = New System.Drawing.Point(13, 128)
    88. Me.lbKey.Name = "lbKey"
    89. Me.lbKey.Size = New System.Drawing.Size(39, 13)
    90. Me.lbKey.TabIndex = 4
    91. Me.lbKey.Text = "Label5"
    92. '
    93. 'lbNumKeyboards
    94. '
    95. Me.lbNumKeyboards.AutoSize = True
    96. Me.lbNumKeyboards.Location = New System.Drawing.Point(13, 159)
    97. Me.lbNumKeyboards.Name = "lbNumKeyboards"
    98. Me.lbNumKeyboards.Size = New System.Drawing.Size(39, 13)
    99. Me.lbNumKeyboards.TabIndex = 5
    100. Me.lbNumKeyboards.Text = "Label6"
    101. '
    102. 'lbVKey
    103. '
    104. Me.lbVKey.AutoSize = True
    105. Me.lbVKey.Location = New System.Drawing.Point(12, 187)
    106. Me.lbVKey.Name = "lbVKey"
    107. Me.lbVKey.Size = New System.Drawing.Size(39, 13)
    108. Me.lbVKey.TabIndex = 6
    109. Me.lbVKey.Text = "Label7"
    110. '
    111. 'Form1
    112. '
    113. Me.ClientSize = New System.Drawing.Size(292, 269)
    114. Me.Controls.Add(Me.lbVKey)
    115. Me.Controls.Add(Me.lbNumKeyboards)
    116. Me.Controls.Add(Me.lbKey)
    117. Me.Controls.Add(Me.lbDescription)
    118. Me.Controls.Add(Me.lbName)
    119. Me.Controls.Add(Me.lbType)
    120. Me.Controls.Add(Me.lbHandle)
    121. Me.Name = "Form1"
    122. Me.ResumeLayout(False)
    123. Me.PerformLayout()
    124. End Sub
    125. Friend WithEvents lbType As System.Windows.Forms.Label
    126. Friend WithEvents lbName As System.Windows.Forms.Label
    127. Friend WithEvents lbDescription As System.Windows.Forms.Label
    128. Friend WithEvents lbKey As System.Windows.Forms.Label
    129. Friend WithEvents lbNumKeyboards As System.Windows.Forms.Label
    130. Friend WithEvents lbVKey As System.Windows.Forms.Label
    131. End Class
    132. End Namespace