2

In a Cocoa app, given this example NSString:

Héllö

is there a way to tell which character is not represented by a physical key or key combination (Shift/Ctrl) on the keyboard?

For example, on a keyboard with US layout, the keys "é" and "ö" are not represented by a key, but on a French keyboard there is a "é" key and on a German keyboard there is a "ö" key.

Basically, I need to reduce the input string to only those characters, which cannot be typed using the current physical keyboard layout.

I think the Text Input Source Services Framework might be the right way to go, but I got stuck there.

Note: I'm aware that some characters can be produced by pressing Option-e/n/i.

Mark
  • 6,647
  • 1
  • 45
  • 88
  • You can fairly easily type accented characters on a US English keyboard. For "ö", hold "o" until a menu appears, then choose the one with the diaeresis. Or type option-u followed by "o". Dunno if these sorts of things affect your requirements... – rickster Oct 29 '14 at 07:09
  • @rickster Yes, I'm aware of that, but I still need to identify those characters in the string (they are used elsewhere in the UI) – Mark Oct 29 '14 at 11:30

2 Answers2

1

You can convert a keyboard character into a CGKeyCode using the strategy from this question:

How to convert ASCII character to CGKeyCode?

The CGKeyCode you get back will refer to a physical key on the keyboard. Use the constants from event.h to see where these fall on a U.S. QWERTY keyboard: kVK_ANSI_A, kVK_ANSI_S, kVK_ANSI_D, etc.

Where can I find a list of Mac virtual key codes?

I imagine (but have not tested) that a German keyboard will return kVK_ANSI_Semicolon when the ö key is pressed. (Same location as ; on a U.S. QWERTY keyboard). This would prove that the key is a physical key on the German keyboard.

When you try the same using the U.S. QWERTY layout, ö will likely return UINT16_MAX, which is the default/error return value from the keyCodeForChar() function from the first link. This would prove that ö is not a physical key in the U.S. QWERTY keyboard layout.

I'm not exactly sure how this will work when faced with keys that are physically different from the keys on a U.S. QWERTY keyboard. The additional letter key to the left of z that is common in Europe, for example. I'm hopeful that that key would be given a kVK_ANSI_ constant as well, in which case this strategy should work.

Community
  • 1
  • 1
pkamb
  • 33,281
  • 23
  • 160
  • 191
0

I think that the only way is to godown to uchr.

You can get a input source property with TISGetInputSourceProperty() and pass kTISPropertyUnicodeKeyLayoutData as property name. Than you will get a keyboard layout in uchr format, which is described here.

I've never tried it, but to me it looks like the intended way.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50