Keyboard layout change in cocoa application
I'm a newbie,
I'm trying to learn objective-c.I have developed a small application which accepts user input and saves it in sqllite. Curently my application supports only english keyboard layout. I have put radiobutton with options English and Danish. On option select the keyboard layout should change to desired on and accept the input.
Can anybody help be out开发者_开发百科with this.
Thank in advance.
Are you saving raw key codes? As if not, the layout is completely uninteresting to you, Cocoa will convert any combination of key codes into Unicode characters that you can save. That way your code will work with whatever exotic keyboard layout the user might have (e.g. Dvorak or Colemak are also English keyboard layouts).
However, to answer your questions, yes, you can get notifications whenever the layout changes. For 10.5 or 10.6 use Text Input Services. The notification is named kTISNotifySelectedKeyboardInputSourceChanged and is sent whenever the user picks another keyboard layout (the other notification is kTISNotifyEnabledKeyboardInputSourcesChanged, this is one is sent whenever the user adds or removes layouts from the list of selectable layouts). It's a normal CF distributed notification that you can receive in your application like all CF distributed notifications. See documentation for CFNotificationCenter.
Maybe those notifications are also receivable as NSNotifications via NSNotificationCenter, but that is not documented anywhere as far as I can see. Using CF (Core Foundation) API in a Cocoa application is no problem, though. You can easily wrap all necessary code into a nice Obj-C object.
精彩评论