NSEvent keyCode list?

Discussion in 'Mac Programming' started by John Baughman, Sep 5, 2009.

  1. John Baughman macrumors regular

    Oct 27, 2003
    Is there a list of keyCodes that is returned by NSEvent keyCode. Using NSLog to see what keyCodes return for various keys doesn't look familiar to me. The documentation describes them as a "virtual key code". What does that mean?

  2. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    Virtual key codes should be the same as those used by Carbon which are located in <HIToolbox/Events.h>

    Extracted and include for future visitors:

    	 *  Summary:
    	 *    Virtual keycodes
    	 *  Discussion:
    	 *    These constants are the virtual keycodes defined originally in
    	 *    Inside Mac Volume V, pg. V-191. They identify physical keys on a
    	 *    keyboard. Those constants with "ANSI" in the name are labeled
    	 *    according to the key position on an ANSI-standard US keyboard.
    	 *    For example, kVK_ANSI_A indicates the virtual keycode for the key
    	 *    with the letter 'A' in the US keyboard layout. Other keyboard
    	 *    layouts may have the 'A' key label on a different physical key;
    	 *    in this case, pressing 'A' will generate a different virtual
    	 *    keycode.
    	enum {
    	  kVK_ANSI_A                    = 0x00,
    	  kVK_ANSI_S                    = 0x01,
    	  kVK_ANSI_D                    = 0x02,
    	  kVK_ANSI_F                    = 0x03,
    	  kVK_ANSI_H                    = 0x04,
    	  kVK_ANSI_G                    = 0x05,
    	  kVK_ANSI_Z                    = 0x06,
    	  kVK_ANSI_X                    = 0x07,
    	  kVK_ANSI_C                    = 0x08,
    	  kVK_ANSI_V                    = 0x09,
    	  kVK_ANSI_B                    = 0x0B,
    	  kVK_ANSI_Q                    = 0x0C,
    	  kVK_ANSI_W                    = 0x0D,
    	  kVK_ANSI_E                    = 0x0E,
    	  kVK_ANSI_R                    = 0x0F,
    	  kVK_ANSI_Y                    = 0x10,
    	  kVK_ANSI_T                    = 0x11,
    	  kVK_ANSI_1                    = 0x12,
    	  kVK_ANSI_2                    = 0x13,
    	  kVK_ANSI_3                    = 0x14,
    	  kVK_ANSI_4                    = 0x15,
    	  kVK_ANSI_6                    = 0x16,
    	  kVK_ANSI_5                    = 0x17,
    	  kVK_ANSI_Equal                = 0x18,
    	  kVK_ANSI_9                    = 0x19,
    	  kVK_ANSI_7                    = 0x1A,
    	  kVK_ANSI_Minus                = 0x1B,
    	  kVK_ANSI_8                    = 0x1C,
    	  kVK_ANSI_0                    = 0x1D,
    	  kVK_ANSI_RightBracket         = 0x1E,
    	  kVK_ANSI_O                    = 0x1F,
    	  kVK_ANSI_U                    = 0x20,
    	  kVK_ANSI_LeftBracket          = 0x21,
    	  kVK_ANSI_I                    = 0x22,
    	  kVK_ANSI_P                    = 0x23,
    	  kVK_ANSI_L                    = 0x25,
    	  kVK_ANSI_J                    = 0x26,
    	  kVK_ANSI_Quote                = 0x27,
    	  kVK_ANSI_K                    = 0x28,
    	  kVK_ANSI_Semicolon            = 0x29,
    	  kVK_ANSI_Backslash            = 0x2A,
    	  kVK_ANSI_Comma                = 0x2B,
    	  kVK_ANSI_Slash                = 0x2C,
    	  kVK_ANSI_N                    = 0x2D,
    	  kVK_ANSI_M                    = 0x2E,
    	  kVK_ANSI_Period               = 0x2F,
    	  kVK_ANSI_Grave                = 0x32,
    	  kVK_ANSI_KeypadDecimal        = 0x41,
    	  kVK_ANSI_KeypadMultiply       = 0x43,
    	  kVK_ANSI_KeypadPlus           = 0x45,
    	  kVK_ANSI_KeypadClear          = 0x47,
    	  kVK_ANSI_KeypadDivide         = 0x4B,
    	  kVK_ANSI_KeypadEnter          = 0x4C,
    	  kVK_ANSI_KeypadMinus          = 0x4E,
    	  kVK_ANSI_KeypadEquals         = 0x51,
    	  kVK_ANSI_Keypad0              = 0x52,
    	  kVK_ANSI_Keypad1              = 0x53,
    	  kVK_ANSI_Keypad2              = 0x54,
    	  kVK_ANSI_Keypad3              = 0x55,
    	  kVK_ANSI_Keypad4              = 0x56,
    	  kVK_ANSI_Keypad5              = 0x57,
    	  kVK_ANSI_Keypad6              = 0x58,
    	  kVK_ANSI_Keypad7              = 0x59,
    	  kVK_ANSI_Keypad8              = 0x5B,
    	  kVK_ANSI_Keypad9              = 0x5C
    	/* keycodes for keys that are independent of keyboard layout*/
    	enum {
    	  kVK_Return                    = 0x24,
    	  kVK_Tab                       = 0x30,
    	  kVK_Space                     = 0x31,
    	  kVK_Delete                    = 0x33,
    	  kVK_Escape                    = 0x35,
    	  kVK_Command                   = 0x37,
    	  kVK_Shift                     = 0x38,
    	  kVK_CapsLock                  = 0x39,
    	  kVK_Option                    = 0x3A,
    	  kVK_Control                   = 0x3B,
    	  kVK_RightShift                = 0x3C,
    	  kVK_RightOption               = 0x3D,
    	  kVK_RightControl              = 0x3E,
    	  kVK_Function                  = 0x3F,
    	  kVK_F17                       = 0x40,
    	  kVK_VolumeUp                  = 0x48,
    	  kVK_VolumeDown                = 0x49,
    	  kVK_Mute                      = 0x4A,
    	  kVK_F18                       = 0x4F,
    	  kVK_F19                       = 0x50,
    	  kVK_F20                       = 0x5A,
    	  kVK_F5                        = 0x60,
    	  kVK_F6                        = 0x61,
    	  kVK_F7                        = 0x62,
    	  kVK_F3                        = 0x63,
    	  kVK_F8                        = 0x64,
    	  kVK_F9                        = 0x65,
    	  kVK_F11                       = 0x67,
    	  kVK_F13                       = 0x69,
    	  kVK_F16                       = 0x6A,
    	  kVK_F14                       = 0x6B,
    	  kVK_F10                       = 0x6D,
    	  kVK_F12                       = 0x6F,
    	  kVK_F15                       = 0x71,
    	  kVK_Help                      = 0x72,
    	  kVK_Home                      = 0x73,
    	  kVK_PageUp                    = 0x74,
    	  kVK_ForwardDelete             = 0x75,
    	  kVK_F4                        = 0x76,
    	  kVK_End                       = 0x77,
    	  kVK_F2                        = 0x78,
    	  kVK_PageDown                  = 0x79,
    	  kVK_F1                        = 0x7A,
    	  kVK_LeftArrow                 = 0x7B,
    	  kVK_RightArrow                = 0x7C,
    	  kVK_DownArrow                 = 0x7D,
    	  kVK_UpArrow                   = 0x7E
    	/* ISO keyboards only*/
    	enum {
    	  kVK_ISO_Section               = 0x0A
    	/* JIS keyboards only*/
    	enum {
    	  kVK_JIS_Yen                   = 0x5D,
    	  kVK_JIS_Underscore            = 0x5E,
    	  kVK_JIS_KeypadComma           = 0x5F,
    	  kVK_JIS_Eisu                  = 0x66,
    	  kVK_JIS_Kana                  = 0x68
  3. John Baughman thread starter macrumors regular

    Oct 27, 2003
    lloyddean, thanks!

    I am obviously a new-bee to Mac Programming going through Hiillegass's book. What I am finding very frustrating is being able to lookup references for code not found in the developer documentation that comes with Xcode and used without explanation in the book.

    For example in Chapter 31 we start using CoreGraphics and we use a data type of CGFloat. The book describes this as a C array and is in fact treated as an array. To make this clearer for me, I tried to find a reference for CGFloat. The Xcode developer reference did not find it. It took me some time to find the reference for Core Graphics where I do not see any reference to CGFloat as an C array data type. So how, without the book telling me, would I know that "CGFloat *terms" defines terms as a C array?

    Obviously I do not understand C let alone Objective C, but the issue here is my inability to find the proper documentation to help me figure things out on my own.

    There is no single reference that I can find for Cocoa programming. Do you have any suggestions on the best approach to looking things up in all the available documentation?


  4. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    John, sorry I lost track of this thread.

    Could you tell me:

    • What Mac experience you do have as a user?
    • What version of Mac OS X are you running?
    • What version of Xcode are you using?
    • Do you have any programming experience?
  5. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA

    Attached is a Xcode project file we will be using for you to follow along with. Once downloaded you'll need to do a little work to prepare the project for what is to follow.

    1. Open the Xcode project file "Faux.xcodeproj"
    2. In the Xcode "Window" menu select the "Activity" menu item.
    3. In the Xcode "Project" menu select "Edit Project Settings"
    4. Select the "General" tab
    5. Click the "Rebuild Code Sense Index" button
    6. Click the "Rebuild Index" button that appears in the sheet.

    Look at the content area of the "Activity" window that appeared at step 2. Once the "Project Index" task completes go ahead and dismiss the "Activity" window.

    Dismiss the Xcode Preferences.

    A "lookup" table of all enum's, macro's, API declarations and more will have been built allowing for symbol completions and symbol lookups. The symbol lookup is built by parsing source and header files contained in the project.

    If you'll look at the contents of the file "main.mm" you'll note the many #import directives. I've place them here in order for the "Code Sense" parser to index giving you access to most every item you're likely to want to look up.

    Next note the line "CGFloat realNum;" located in the main function. While holding down the command key double-click "CGFloat". If everything went correctly the file "CGBase.h" should've opened with the line containing the definition of 'CGFloat' selected.

    Anytime you don't know where or what any System API data type or function name is it can be found by typing it's name with in the main function of main.mm and command-double-clicking it.

    Be sure you don't accidentally modify and save changes to the System headers.

    Questions ?

    Attached Files:

    • Faux.zip
      File size:
      12.5 KB
  6. John Baughman thread starter macrumors regular

    Oct 27, 2003
    lloyddean - Thanks!!

    I am having a problem in that after following your instructions, command double-clicking does nothing. I did notice that when rebuilding the codes sense index it flashed very quickly, which I would have expected to take a while considering that you have all the frameworks defined.

    Does the project need to be built? If so it will not build because it appears that you are working in 10.6 and I have not yet upgraded to Snow Leopard. I believe this is true because the Build throws up an error that it cannot find the macosx10.6 SDK.

    Could this be the problem ie either the fact that I cannot build the project or that you created the project in Snow Leopard?

  7. John Baughman thread starter macrumors regular

    Oct 27, 2003
    To answer you questions...

    • What Mac experience you do have as a user?
      Mac user since the very beginning.
    • What version of Mac OS X are you running?
      OS 10.5.8
    • What version of Xcode are you using?
    • Do you have any programming experience?
      Many years of programming experience but I am a 4th Dimension database developer. No experience with c, c++, or objective-c. My expertise is in the 4th Dimension RDBMS, and I have some experience with SQL, ASP.net, and a few other obscure programming environments.

  8. John Baughman thread starter macrumors regular

    Oct 27, 2003
    lloyddean - I figured out what was wrong. I had to change the Base SDK for all Configurations popup to 10.5.

    OK now that I have your project working properly I am not sure that this is exactly what I am looking for. Taking CGFloat and my initial question about it, how do I know that CGFloat is an array? The definition only defines it as a float and lists the Informational Macros for it. How can I quickly find a plain english description for CGFloat?

    Option-double-clicking something is great, but when it is not found in the Developer Docs that came with Xcode, I seem to spend way more time searching the net for documentation than should be necessary. Which tells me I am doing it wrong.

  9. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    May I ask 'why' you want to know what 'CGFloat' is?

    What differences does it make to you?

    It's a data type defined, used and (for the most part) manipulated by Core Graphics.

    Just need to know where you're coming from in order to answer the question in a way that might satisfy you.

    For the most part Obj-C, being derived from 'C', makes use of base types as building blocks to assemble more complex data types.

    'C' intrinsics include - 'char', 'short int', 'int', 'long int', 'long long int', 'float', 'double' and 'long double'.

    The 'size' of each is dependent upon the target platform, CPU and compiler settings.

    What that means is that CGFloat maps to a 'C' float when compiling to a 32-bit environment, OR a double on a 64-bit environment.

    The fact is you don't know, or care, unless you're writing an interface to an external API, or writing code to archive data to a file for exchange with another program.
  10. John Baughman thread starter macrumors regular

    Oct 27, 2003
    lloyddean - You posted just before I was about to post an apology. I woke up early this morning with the realization that I was really having a brain fart over CGFloat. I was being thrown off by the following code in one of Hillegass' exercises

    terms = NSAllocateCollectable(termCount * sizeof(CGFloat), 0);		
    int i;
       for (i=0; i < termCount; i++) {
       terms[i] = 5.0 - (random() % 100) / 10.0;
    terms is a CGFloat. When I saw size of(CGFloat) and terms I stupidly jumped to the conclusion that I was dealing with an array which did not make sense to me and so I was looking for a discussion of CGFloat. After some review this morning, I now see that we are dealing with collectable memory. CGFloat is a float just like the definition says it is. duhh!

    The original reason of this post was to locate a list of keyCodes that is returned by NSEvent keyCode. That led to my question regarding the best way to look things up in the available documentation and I only used CGFloat as an example of my frustration in finding things from within xCode... a bad example to say the least.

    Thanks for sticking with me on this. I am sorry to have wasted your time.

    BTW, can you correct me if I am wrong in my understanding of the above code...

    sizeof(CGFloat) returns 4 and termCount is 3. So we are allocating 12 bytes for the collection, enough room for 3 floats each with a maximum size of 4 bytes each which is the maximum size needed to store a CGFloat. We are then storing 3 random floats as terms[0], terms[1] and terms[2].

    I am not really clear on how to use collectable memory. As far as I can tell the collection only knows it's size, not the size of each collectable nor how many collectables there will be. Does it just keep track of the start and end points of [0], [1], and [2]?

    Thanks again for your help. I learned a lot from the discussion and your look-up project will be very helpful to me as I struggle on.

  11. robbieduncan Moderator emeritus


    Jul 24, 2002
    I'll put money on terms not being a CGFloat, rather being a pointer to a CGFloat which is a totally different thing.
  12. John Baughman thread starter macrumors regular

    Oct 27, 2003
    Your are absolutely right. I should have referred to terms as a pointer.

    I still do not have my head wrapped around pointers as pointing in Objective-C. In my previous programming experience pointers were thought of as simply pointing to another variable. De-referencing the pointer gave us the value of the other variable. What was really going on was hidden from the me the programmer. We did not have to think in terms of memory allocation and addressing.

    It's been hard for me to grasp the idea of creating a pointer to a type or an object and then later addressing the pointer to an address in memory that holds a value or allocating and initializing memory for the pointer that is yet to be filled.
  13. FelicityW macrumors regular

    Apr 4, 2011
    Now that's what I'm talking about! (And I am putting it in a pretty little table, 'cause that's what I do.)
    Thank you!

Share This Page

12 September 5, 2009