Where is the list of the key code?

When building a tool and using onKeyDown and onKeyUp, a key is passed as a parameter.

But where is the list of key code for this parameter type ?

I was expecting a list of constants in the API docs but there are only the modifiers.

Did I miss something ?

The key codes are platform dependant. The value is passed directly from the operation system, so you’d have to look up the documentation for the platform itself.

So, I didn’t miss anything.

Warning, a rant is coming. Too much coffee this morning.

I understand the problem but IMHO, this is even worse because the documentation doesn’t give this critical piece of info (platform dependent key code). I assumed (and I suppose many other did) that the key codes were at least platform independent.
So my code is broken on mac.

The case for a Key module is even stronger. To be multiplatform, I’ll have to check both the platform and the returned code.

It would be easier to have a Key module filled the current platform codes at Sketchup launch, so one could do key== Sketchup.Key.K. It is really strange for an api to use a set (key codes) and not giving.the possible values it can takes.

Rant off. I know resources are not unlimited. And you’re doing your best.

Thank you for your answer.

FYI, the key codes for MS Windows are easily found at MSDN:

Here is a snippet of Ruby code:

  if keycode.between?(0x30,0x39) # 48..57 decimal
    # It is a top row number key 0..9:
    "#{keycode-48}"
  elsif keycode.between?(0x60,0x69) # 96..105 decimal
    # It is a NUMPAD number key 0..9:
    "#{keycode-96}"
  elsif keycode.between?(0x41,0x5A) # 65..90 decimal
    # It is a standard ASCII keycode A..Z:
    "#{keycode.chr}"
  elsif keycode.between?(0x70,0x87) # 112..135 decimal
    # It is a function key between F1 and F24:
    "F#{keycode-111}"
  else
    # lookup call goes here
  end

Well the APi doc for Tool#onKeyDown clearly states:

There are several “virtual keys” defined as constants you can use. Their use is cross platform.

(And then this small set of constants is listed.)

Nothing prevents you from defining your own local VK constants within your own module namespaces. (There are examples in several languages of doing similar to this at the bottom of the MSDN reference page I linked in the previous post.)

Yes a community mixin / library module would be nice. (I’ll see if I can whip something or see if we had previously posted this. I know we have discussed this before at SketchUcation.)
The library module would check the platform at load time, and assign the correct key code to each constant.

That is actually weird, and not “rubyish.”

The proper thing is to define a library / mixin module of constants. (Referencing constants may be faster than making method calls.)

The module would need loading:

# at the top of your Tool class definition:
require "library/keycodes"

Then you can reference any constant in the library module via qualification, like:

refresh_dialog() if keycode == Library::KeyCodes::VK_F5

… or mix ALL those constants (as local proxy lookups) into your own plugin’s Tool class namespace:

# at the top of your Tool class definition:
require "library/keycodes"
include Library::KeyCodes

# and further down in it's `onKey..` callback(s):
refresh_dialog() if keycode == VK_F5

… or just locally define references to only those constants your Tool class actually needs:

# at the top of your Tool class definition:
require "library/keycodes"
VK_F5 = Library::KeyCodes::VK_F5

# and further down in it's `onKey..` callback(s):
refresh_dialog() if keycode == VK_F5

* Note, the names of folders and modules (in these above examples) are purely for example. (Ie, they may not represent the de facto standard for the API.)

@LBarret

The list of Mac OSX keycodes froml C header file “Events.h”:
https://github.com/phracker/MacOSX-SDKs/blob/master/MacOSX10.6.sdk/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h


image from:


I found these as they were referenced by the following (easily found via Google search):

:keyboard:

1 Like

Hi @DanRathbun,
Thank you so much ! I stand by my original opinion ( an api that use a set of values should document/include them) but this answer is so complete that it compensates most of this shortcoming.

1 Like