Key Numbers on MAC OS

Dear Friends,

      def onKeyDown(key, repeat, flags, view)
        p key
      end

In Windows, if press UP key code prints 38 but in MAC prints 63232.
In Windows, if press alt key code prints 18 but in MAC prints 524288.
In Windows, if press Tab key code prints 9 but in MAC prints nothing.
Are codes in MAC unique? In all Sketchup versions, MAC always prints 63232 for the UP key or not?
Thank you in advance for your help.

Is VK_UP constant. Check constants
Tab key on Mac OS respond only in onKeyUp method.

1 Like

As Curic sayd the best way to utilise keyboard events to use the build in constants described in a #onKeyDown method. (Sure you can use it in #onKeyUp too)

  • VK_ALT
  • VK_COMMAND
  • VK_CONTROL
  • VK_DELETE
  • VK_DOWN
  • VK_END
  • VK_HOME
  • VK_INSERT
  • VK_LEFT
  • VK_MENU
  • VK_NEXT
  • VK_PRIOR
  • VK_RIGHT
  • VK_SHIFT
  • VK_SPACE
  • VK_UP

This give you a relative “bullet proof” solution for both OS… well with some restriction e.g some of them may not respond on onKeyDown, In that case you may use the #onKeyUp.

If you need some additional key you have to be careful, because the user can define a shortcut (you know in Skecthup Perferences/ Shortcuts) winch will have a priority.
E.g. If you will define tab key in your extension, but the user set it to do globally in SU, your code will not get the tab key event, but will be deactivated.

Still if you want to use other keys, one way to do it to define a constants in top of your code e.g.

 #Define keys on MAC/WIN (constants)
  MY_TAB_KEY ||= Sketchup.platform == :platform_osx ? 9 : 9
  MY_F_KEY ||= Sketchup.platform == :platform_osx ? 6 : 70

Then use it e.g.

     def onKeyUp(key, repeat, flags, view)
        if(key == MY_TAB_KEY)
            # do something
        end
        if(key == VK_UP)
            # do something
        end
      end

BTW.
My own conclusion - studying the keys - ended up to avoid use other keys than the following keys in my plugins:

  • VK_CONTROL
  • VK_SHIFT
  • VK_DOWN
  • VK_LEFT
  • VK_RIGHT
  • VK_UP
  • VK_ALT

One more advise:
In #onKeyDown, and #onKeyUp :
Return true to prevent SketchUp from processing the event.

e.g.

def onKeyUp(key, repeat, flags, view)
  if(key == VK_ALT)
    # do something
    return true
  end
end
def onKeyDown(key, repeat, flags, view)
  if(key == VK_ALT)
    # do something
    return true
  end
end
1 Like

Thanks again Dezmo. VK_UP can use in both Windows and MAC OS. Using it, we don’t need to worry about key numbers in MAC OS, which is great. It seems we don’t have VK_ESCAPE or VK_TAB. ESC key has the same code on both platforms and it seems better not to use TAB Key on both platforms.

Be careful!!! Keep in your mind this:
Tool#onCancel instance_method

image

is 9 as you see in my first post :wink:
and on Mac OS respond only in onKeyUp method.

No, it is not. It seems Tab key in MAC control by OS and has different usage. I think we’d better forget using the Tab key in MAC OS and let OS use it.

Fortunately esc key is only a key in both platforms and we can define its usage. For example to draw a wall user can select several breakpoints. I used esc key to delete last breakpoints and it work same in both platforms.

Okay.I just wanted to get your attention.Please check the 3 snippets.

You can implement to the onKeyDown to listen for ESC, but in the onCancel method is already there.

 
  def onKeyDown(key, repeat, flags, view)
    if( key == 27) #ESC key pressed
      delete_last_brake_point()
    end 
  end

  def onCancel(reason, view)
    case reason
    when 0 #ESC key pressed
      do_my_chancel_stuff()
    when 1
      # do something
    when 2
      # do something else
    end
  end

this will do the same as above:

  def onCancel(reason, view)
    case reason
    when 0 #ESC key pressed
      delete_last_brake_point()
      do_my_chancel_stuff()
    when 1
      # do something
    when 2
      # do something else
    end
  end

BUT
When onKeyDown is implemented and returns true, pressing Esc doesn’t trigger onCancel.

  def onKeyDown(key, repeat, flags, view)
    if( key == 27) #ESC key pressed
      delete_last_brake_point()
      return true
    end 
  end

  def onCancel(reason, view)
    case reason
    when 0 #ESC key pressed
      do_my_chancel_stuff() # this will not happen
    when 1
      # do something
    when 2
      # do something else
    end
  end
1 Like

Please see …

SketchUp Extension UX Guidelines: Use Esc to Reset the Tool

2 Likes