C
C#3mo ago
Phill

Parsing Input From Separate Keyboards

I volunteer at a digital makerspace (https://www.seattlesim.com/) that has A LOT of USB keyboards and a lot of people who know how to make games. I'm trying to make something in c# that can parse input from separate keyboards the same way you would with separate controllers. The plan is to let a bunch of people make games with it. I've gotten as far as getting the form to capture the keypress event but nothing in any of the variables will let me tell which keyboard triggered the event. I've also been able to successfully utilize ManagementObjectSearcher(@"SELECT * FROM Win32_Keyboard") but none of the fields give me any clear text identifiers. PnpDeviceID and DeviceID are the same on each individual device but unique among the different devices. I'd like to pull the names I see in the windows device manager. After spending all day trying unsuccessfully use user32.dll I think it's time to stop and ask for help. Am I even going about this the right way?
Seattle Interactive Media-Lab
Seattle Interactive Media-Lab
11 Replies
jcotton42
jcotton423mo ago
@Phill Tom Scott did this for his emoji keyboard, so it's def possible https://www.youtube.com/watch?v=lIFE7h3m40U
Tom Scott
YouTube
The Art of the Bodge: How I Made The Emoji Keyboard
http://tomscott.com - http://twitter.com/tomscott - Here's the behind-the-scenes "how I made the emoji keyboard" video! If you haven't seen the original: http://youtu.be/3AtBE9BOvvk But the thing is, the truth is basically just "I bodged some stuff together". Which gives me the opportunity to tell some stories... AUTOHOTKEY: http://ahkscript.or...
jcotton42
jcotton423mo ago
he used lua macros though, not sure how you'd do it from C#
Phill
PhillOP3mo ago
I'm sure I'm not using the correct windows components. Even if there are no windows components, I should be able to read raw USB input somehow. I've just reached the limits of what I know how to do. I could really use some help.
jcotton42
jcotton423mo ago
Couldn't get google to cooperate, so asked ChatGPT. It pointed to https://learn.microsoft.com/en-us/windows/win32/inputdev/using-raw-input
Using Raw Input - Win32 apps
This section includes sample code for tasks relating to raw input.
jcotton42
jcotton423mo ago
https://chatgpt.com/share/684b9438-4844-8009-a7d6-cacecd1f3584 for reference. This seems plausible, as it looks like you get a device handle for each event.
Phill
PhillOP3mo ago
It is parsing the keypress but it's registering multiple presses. I'll have to figure out some kind of repeat filter. It hallucinated the name part, though. Beter than where I was before. Thanks.
SleepWellPupper
SleepWellPupper3mo ago
For filtering/debouncing etc. inputs, you could look into System.Reactive Specifically, Throttle
Phill
PhillOP3mo ago
My plan is to implement a lock system. Implement an array of locks for each keyboard. The lock will store a boolean indicating a keypress has been processed and a character denoting which key was pressed. Key up event then checks the locks for a state and character match then polls each match and responds with the current key press state.
SleepWellPupper
SleepWellPupper3mo ago
This seems like a way to re-implement rx debouncing. I think you should give it a try before rolling your own
Phill
PhillOP2mo ago
GitHub
GitHub - PhillBenoit/KeyboardGame: 4 player word game
4 player word game. Contribute to PhillBenoit/KeyboardGame development by creating an account on GitHub.
SleepWellPupper
SleepWellPupper2mo ago
You can also post here: #showcase and here: #code-review to gather some feedback.

Did you find this page helpful?