Raw Key Code for Function Keys

I figure out key codes for function keys from F1 to F12 on Mac.

  • F1 : 122
  • F2 : 120
  • F3 : 99
  • F4: 118
  • F5: 96
  • F6: 97
  • F7: 98
  • F8: 100
  • F9: 101
  • F10: 109
  • F11: 103
  • F12: 111

They are not related to ASCII table at all.
Code marked as ‘solution’ or ‘answer’ here doesn’t care of them. They are all converted to a printable characters, ^P. So, a table created with the method in the StackOverflow can’t differentiate each of the function keys. So, if you want to differentiate them, customize value saved there using the table above.

It turned out that the database solution, 4D, also had such values for the function keys.
So, it says that the values above is not only specific to my keyboard, Logitech  K760.

(It’s surprising that 4D is still alive.)

2 responses to this post.

  1. See https://github.com/realmacsoftware/ShortcutRecorder or https://github.com/realmacsoftware/ShortcutRecorder/blob/master/Source/SRKeyCodeTransformer.m in particular. The keyCode you get in NSEvent is generally the key codes the computer gets from the keyboard. They can have different mapping under different keyboard layout. E.g. ‘a’ == 0 under US layout, but if you use French AZERTY keyboard, ‘a’ will be 12. And ‘q’ will be 0, since represented by the same physical key on the keyboard as ‘a’ in the US layout.

    To sum it up – the keyCode is not meant to be ASCII. It’s meant to be the code of the physical key on the keyboard. There are functions that allow you to transfer between the keyboard mapping.

    BTW better way to detect this is to use the “characters” property and check for NSF11FunctionKey – which is a unichar code, which represents F11 in the characters property.


    • Posted by jongampark on February 8, 2016 at 1:31 PM

      Hello, Mr. Monroe

      Sure. I’m not a novice, and I know that it’s not ASCII but hardware keycode. (that is why it’s called ‘keyCode’. ) However, for some characters they happened to be ASCII or some values which can be converted to ASCII.

      Why I wrote ‘it’s not ASCII’ is not because I expect it be ASCII but because it’s not any code you can find on Apple’s document easily. “Being ASCII” is just entrance to all of them.

      Probably it’s better to post in my mother language from now on. Sometimes I use Korean expression in English, which only makes sense to Koreans. So, it may confuse people.

      It’s H/W code to be more specific. Also if you care Windows, you should consider if it’s H/W key or virtual key code. ( virtual key code is also different from ASCII. It’s intermediate code between H/W key code and any code in the level of ASCII or EBSDIC, for example. )

      Anyway, are you Kenzo? https://github.com/Kentzo/ShortcutRecorder

      I integrated Kenzo’s ShortcutRecorder to my project. I think I read that Kenzo’s ShortcutRecorder was sprang off from the ShortcutRecorder you mentioned.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: