Safe Hot Keys

Doesn’t this works for you? (You can use the ALT_MODIFIER_KEY constant instead of VK_ALT too)

class Tool_test_alt
  def onKeyDown(key, rpt, flags, view)
    if( key == VK_ALT ) 
      @alt_down = true
      puts "onKeyDown: key = #{key}"
      view.invalidate
      return true
    end
  end

  def onKeyUp(key, rpt, flags, view)
    if( key == VK_ALT )
      @alt_down = false
      puts "onKeyUp: key = #{key}"
      view.invalidate
      return true
    end
  end

  def draw(view)
    view.draw_text( [0,10], "Alt is pressed: #{@alt_down}" )
  end
  
  def onCancel(reason, view)
    view.invalidate
  end

  def deactivate(view)
    view.invalidate
  end
end
Sketchup.active_model.select_tool(Tool_test_alt.new)

alttest

In both the #onKeyUp and the #onKeyDown you should: " Return true to prevent SketchUp from processing the event."

image

1 Like

Here is an example for using an ALT toggle instead of a ALT holddown …

class ToolTest

  if Sketchup.platform == :platform_win
    WIN = true
    require 'win32ole'
  else
    WIN = false
  end
  
  NORM_MODE ||= 'Do something ... | Click ALT for nifty mode'
  ALT_MODE  ||= 'Do something ... | Click ALT for normal mode'

  def self.use
    Sketchup.focus if Sketchup.respond_to?(:focus)
    Sketchup.active_model.select_tool(self.new)
  end

  def initialize(*args)
    @alt_mode = false
    @shell = WIN32OLE.new('WScript.Shell') if WIN
  end

  def activate
    set_status_text()
    Sketchup.active_model.active_view.invalidate
  end

  def onCancel(reason, view)
    # Gets called before onKeyUp when ESC key is pressed.
    # Gets called after onKeyDown if it does not return true.
    puts "\n#{self.class}: onCancel was called."
  end

  def onKeyUp(key, repeat, flags, view)
    result = false
    case key
    when ALT_MODIFIER_KEY
      puts "\n#{self.class}: ALT was pressed and released"
      # Toggle alternative mode:
      @alt_mode = !@alt_mode
      set_status_text()
      # Send an ESC key to SketchUp on Windows platform. This will
      # clear the menu selection, and onCancel will NOT fire.
      @shell.SendKeys('{ESC}') if WIN
      result = true # to prevent SketchUp from processing the event
    when 9
      puts "\n#{self.class}: TAB key clicked"
      # do something else ...
    else
      # whatever ...
      puts "\n#{self.class}: onKeyUp: key = #{key}"
    end
    puts "  flags = %B" % flags
    return result
  end

  def set_status_text
    Sketchup.status_text=( @alt_mode ? ALT_MODE : NORM_MODE )
  end

end

P.S. - I’m not sure why the Windows Scripting Host SendKeys method also sends a É (144) character after the ESC. But it should be ignored (if you don’t write a when clause for it.)

The Tab key is obviously problematic. I’m going to test out some of this code with the Alt key and see what I can do with it.

P.S.
Adding the single line “return true” seems to fix the problem with the Alt key pausing SketchUp. It looks like the Alt key will now be my new secondary hotkey goto when I’ve already commandeered the Control key.

Thank-you Dezmo on this very simple hint but solution. I just need to have my Mac users test it for me to verify that it works as intended on MacOS.

2 Likes

I did not notice any difference myself with return value.
I found that if the ESC key is not sent (to clear the menu focus,) then the status bar is not updated and is actually wiped blank.