Key events from specific keyboard?

I know I can get KeyUp and KeyDown events no problem, but can I get them for a specific connected device? For example I want to be able to distinguish input from a Laptop’s own keyboard and a keyboard connected to the Laptop via USB. If it is not a laptop computer, I want my app to be able to simply distinguish which keyboard is being used if more than one keyboard is connected to the computer. I need this functionality for both Mac and Windows. Any way to do it with Xojo? I also use full MBS plugins. Thanks for any help.

For Mac you can NSEventMBS class. There is a NSApplicationMBS.currentEvent method to get current event in the keydown/up event in Xojo.

This has more infos on the event.

I’ll add you a new CGEventSourceMBS class to check keyboard type:

[code]dim a as NSApplicationMBS = NSApplicationMBS.sharedApplication
dim e as NSEventMBS = a.currentEvent
dim v as CGEventMBS = new CGEventMBS(e.CGEventRef)
dim s as CGEventSourceMBS = v.EventSource
dim t as integer = s.KeyboardType

Title = str(t)[/code]

That would be great! I had not gotten very far …

dim a as new NSApplicationMBS dim n as NSEventMBS = a.currentEvent msgbox n.description

With current NS properties it appears impossible to get any info on the specific keyboard.

For CGEventSourceMBS, please wait for next prerelease, probably tomorrow.

Sure, and many thanks in advance for your work!

Thank you! I just downloaded beta plugins to test …

The good news is that CGEventSourceMBS.KeyboardType (integer) does report a different number for the MacBook keyboard (43) versus an external keyboard (40)

Bad news is that all connected external keyboards report the same KeyboardType (40), so it is so far not possible to distinguish between multiple connected keyboards. I connected one keyboard directly by USB and a different one at the same time using bluetooth, and they both report 40.

I check Apple documentation for CGEventSource and didn’t find anything that looked more promising. There must be some low-level hardware information somewhere. Maybe the only way to get it will be looking into the USB devices and Bluetooth devices and monitoring traffic there? I don’t know how to do that but it seems logical… but also complicated.

and the handle property doesn’t help?
e.g. keep the sources you have seen and compare to new sources?

[quote=415304:@Christian Schmitz]and the handle property doesn’t help?
e.g. keep the sources you have seen and compare to new sources?[/quote]

No, the Handle is always changing for every keystroke on every keyboard. Same with Hash.

Well, at least you can differentiate between internal and external keyboards for a notebook.
That is what I use it for.

[quote=415307:@Christian Schmitz]Well, at least you can differentiate between internal and external keyboards for a notebook.
That is what I use it for.[/quote]

Yes, it’s useful! Also I do find a difference when I plug in an Apple Extended Keyboard with number-pad (34) versus a slim MackBook-style keyboard (40). So I can even get what I am looking for in a limited way.

So the KeyboardType is just what it says, the general type of keyboard it is, not the specific kind but the general category. I wonder if there is a list of these numbers anywhere and what they mean? So far I have:

40 = external MacBook-style slim keyboard (no number pad) *
43 = internal MacBook keyboard
34 = external Apple Extended Keyboard (old white iMac type)

  • it’s also 40 for a Windows slim keyboard, so the OS doesn’t care if “option” is “alt” or “Command” is “Windows” etc.

… the question remains whether this can also be done on Windows.

On Mac I’ve found out how to get exclusive input from at least 3 simultaneously connected keyboards (maybe more, but I don’t have more keyboards to test). This takes some fiddling with System Preferences Keyboard settings.

First, I found the keyboard identifier numbers are stored in /Library/Preferences/com.apple.keyboardtype.plist However, changing these numbers manually in the plist file has no effect. Maybe I did this wrong or it has to do with file access privileges, but the only way I found to change the identifier used for a given keyboard was to go through a step by step process at System Preferences > Keyboard. Here are the steps:

  1. disconnect all keyboards except the one for which you want to change the type
  2. click the button in the lower left corner “Change Keyboard Type…”
  3. go through the keyboard identification process, and at the end make sure you select a type that does not belong to any of the other keyboards you want to use.

Interestingly, I found that identifying keyboards as “Japanese” for example does not change the characters that come out when typed. This doesn’t correspond to what I read online about this. Users sometimes have problems with the keyboard typing the wrong characters, and it turns out it’s managing this keyboard type that fixes their problem.

Anyway, so now I’ve found a way to do this, it’s not exactly user-friendly. Directing users to go through this process could potentially result in problems if they aren’t savvy users. Though that might remain true even if I can control the setup process, I’m wondering if Xojo can be used to do what is being done there by the System Preferences keyboard type setup? Any ideas?