views:

58

answers:

1

Hi,

I am currently trying to get simple keyboard input on OSX, right now I am doing it through the Leopard HID Manager object and that generally works, but since that is pretty low level I am wondering if there is an API available that has some extra functionality build it like key repetition or Unicode support (since when I catch the events on HID I/O level I think I have to write all these fancy extras from scratch). I know carbon event handlers(NewEventHandlerUPP) are capable of that but I am pretty sure that they are deprecated since you can't find anything about them in the current OSX reference and I don't want to use anything deprecated, so I am wondering if there is any alternative I didn't come across during my search!

Thanks!

+1  A: 

No.

At the Unicode level, the official API of receiving input is NSTextInputClient protocol in Objective-C, and the official API of processing input between the keyboard and the program is the Input Method Kit.

And you can never write a sufficiently fancy extra correctly from scratch. You need to get the user's setting of the international keyboard and modify the key obtained accordingly. And you can never write an input method from scratch which turns the raw key input to Chinese or Japanese ...

So, I think the sane choices are either

  1. Just get the raw ASCII data from the keyboard and don't aim for more, or
  2. Use Cocoa at least around the key input handling, to get additional features.
Yuji
Hey, thanks, can't I convert my raw USB HID input to the OSX Virtual Key Codes and then translate the key depending on the language of the keyboard with http://developer.apple.com/library/mac/#documentation/Carbon/Reference/Unicode_Utilities_Ref/Reference/reference.html . That was my idea so far!
`UCTranslate` doesn't handle generic input methods. It only works with keyboards where the key-to- character mapping is unique, like European ones. East asian keyboards won't work. And, good luck getting the `UCKeyboardLayout` structure needed to use it; that's based on ancient `rsrc` system, which is now superseded by .keylayout XML files. I don't know any easy-to-use API which converts the XML to the `rsrc` needed. Seriously, just use Cocoa for the text input.
Yuji
this was my idea : TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); CFDataRef layoutData = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData); const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);
Aah, I see. Thanks for the info :) Why do you use the HID input in the first place?
Yuji
well I want to be able to use multiple keyboards at once, and also design the functionality of the c++ keyboard class with portabilty in mind. Cocoa does not really integrate as well in that case. While wrapping cocoa was fine for my window classes, I could not really make it work in a good way in cocoa for the keyboard, so I went with the HID manager.
Can't you use multiple keyboards out of the box? I can with my machine. And if you consider portability in mind, please think of East Asians where your approach simply doesn't work :p There, another level of translation from phonetic unicode characters to logographic unicode characters is done by the input method.
Yuji
Also, quartz event taps http://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html might be more suitable than HID. It gives you the keyboard events by the unicode characters.
Yuji
well but I think cocoa does not differ which events came from which keyboard (or does it?), the quartz thing looks like another interesting thing to check out!
Aaah, I see. You want to distinguish which keyboard the event came. That would be a big job ...
Yuji