ohueter/autokbisw

Filter non-keyboard events from Logitech G915 USB Dongle

Closed this issue · 5 comments

Hello, not an issue, but more of a question.

I have a mac mini m1, I have two keyboards on it. One is in English and another one in Spanish. Mostly because I have a K400 that has a trackpad and makes my life easier to control my computer when I'm a bit far.

My idea was that autokbisw would automatically switch between keyboards depending of which one was in use, but so far not much in terms of luck. If for example I start typing using my Spanish keyboard and manually switch to Spanish then go to to my English keyboard it will stay in Spanish. I have tried a few alterations, but so far nothing has really worked. I'm starting to think that this only works with laptops and a single external keyboard perhaps?

thanks!

Hi @fcastro86, autokbisw definitely works with two separate external keyboards, I'm using it that way myself :-)

autokbisw does not automatically recognize the input language it should use. What you need to do would be:

  1. Type a letter on the english keyboard
  2. Set input language to English
  3. Type a letter on the spanish keyboard
  4. Set input language to Spanish

Only then autokbisw memoizes these settings and restores them when using the corresponding keyboard. Would you try following these steps and report back if it worked for you?

Hi, just tried that, but it it behaves a bit funky so decided to record it:
https://drive.google.com/file/d/1-JIT6ZWG4JdVGfxzjBCDunG17v55AFzA/view?usp=sharing

Are there logs anywhere I can look at? thanks!

Logging is not enabled by default, but you can enable verbose logging to stdout via autokbisw -v 2 (see autokbisw -h for help). You should stop the autokbisw service before running it manually.

I think I may have found the issue. My keyboard logitech g915 seems to always be spaming, if I unplug the dongle it stops printing lines in the log. I will try with a different set of keyboards to confirm if this is the problem, thanks:

Here is part of the log:

received event from keyboard USB Receiver-[1133-50497-Logitech-unknown] - 35848192 - 4295001272
set input source to <TSMInputSource 0x6000008edd20> KB Layout: Spanish (id=87) for keyboard USB Receiver-[1133-50497-Logitech-unknown]
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
set input source to <TSMInputSource 0x6000008edd00> KB Layout: U.S. International – PC (id=15000) for keyboard USB Receiver-[1133-50475-Logitech-unknown]
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50497-Logitech-unknown] - 35848192 - 4295001272
set input source to <TSMInputSource 0x6000008eb8a0> KB Layout: Spanish (id=87) for keyboard USB Receiver-[1133-50497-Logitech-unknown]
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
set input source to <TSMInputSource 0x6000008eb900> KB Layout: U.S. International – PC (id=15000) for keyboard USB Receiver-[1133-50475-Logitech-unknown]
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50475-Logitech-unknown] - 18104320 - 4294999341
received event from keyboard USB Receiver-[1133-50497-Logitech-unknown] - 35848192 - 4295001272
set input source to <TSMInputSource 0x6000008edc80> KB Layout: Spanish (id=87) for keyboard USB Receiver-[1133-50497-Logitech-unknown]
received event from keyboard USB Receiver-[1133-50497-Logitech-unknown] - 35848192 - 4295001272
received event from keyboard USB Receiver-[1133-50497-Logitech-unknown] - 35848192 - 4295001272

If I understand that right, you are using a wireless keyboard connected via an USB dongle, and the keyboard/dongle is causing repeating keyboard input events although you did not press a button. Is that correct?

Maybe you could try to find out more about these specific events and if we can block/ignore them in the myHIDKeyboardCallback:

private func registerHIDKeyboardCallback(context: UnsafeMutableRawPointer) {
let myHIDKeyboardCallback: IOHIDValueCallback = {
context, _, sender, _ in
let selfPtr = Unmanaged<IOKeyEventMonitor>.fromOpaque(context!).takeUnretainedValue()
let senderDevice = Unmanaged<IOHIDDevice>.fromOpaque(sender!).takeUnretainedValue()
let conformsToMouse = IOHIDDeviceConformsTo(senderDevice, UInt32(kHIDPage_GenericDesktop), UInt32(kHIDUsage_GD_Mouse))
let vendorId = IOHIDDeviceGetProperty(senderDevice, kIOHIDVendorIDKey as CFString) ??? "unknown"
let productId = IOHIDDeviceGetProperty(senderDevice, kIOHIDProductIDKey as CFString) ??? "unknown"
let product = IOHIDDeviceGetProperty(senderDevice, kIOHIDProductKey as CFString) ??? "unknown"
let manufacturer = IOHIDDeviceGetProperty(senderDevice, kIOHIDManufacturerKey as CFString) ??? "unknown"
let serialNumber = IOHIDDeviceGetProperty(senderDevice, kIOHIDSerialNumberKey as CFString) ??? "unknown"
let locationId = IOHIDDeviceGetProperty(senderDevice, kIOHIDLocationIDKey as CFString) ??? "unknown"
let uniqueId = IOHIDDeviceGetProperty(senderDevice, kIOHIDUniqueIDKey as CFString) ??? "unknown"
let keyboard = selfPtr.useLocation
? "\(product)-[\(vendorId)-\(productId)-\(manufacturer)-\(serialNumber)-\(locationId)]"
: "\(product)-[\(vendorId)-\(productId)-\(manufacturer)-\(serialNumber)]"
if selfPtr.verbosity >= TRACE {
print("received event from keyboard \(keyboard) - \(locationId) - \(uniqueId)")
}
if conformsToMouse {
if selfPtr.verbosity >= TRACE {
print("ignoring event as device is a mouse")
}
selfPtr.onKeyboardEvent(keyboard: "CONFORMS_TO_MOUSE")
} else {
selfPtr.onKeyboardEvent(keyboard: keyboard)
}
}
IOHIDManagerRegisterInputValueCallback(hidManager, myHIDKeyboardCallback, context)
}
}