My tool and problem with bind ALT in Windows

In Windows, the ALT key takes you to the top menu.
When you create your own tool and want to add actions to ALT (onKeyUp func), problems appear

I solved this problem, but in an aggressive way.

How I implemented it: using fiddle and WinApi I put a global interceptor (Hook) on the keyboard (WH_KEYBOARD_LL), catch ALT and replace it with my function

Attention: If my tool is active, then ALT does not work in other windows programs. I think you can put another type of interceptor that will perform the same role, but only when the Sketchup window is active. But that is another story…

Maybe I’m a fool and there is a more cultural method, because there is no such problem with standard tools (Move tool & etc).

I wrote some code for demonstration. Paste into Console. Return to the model. Don’t change the tool. Don’t move your mouse. press ALT. The cursor will change.

require 'fiddle/import'

class Alt_key
 def activate
	@dll = Fiddle.dlopen('user32')
	setWindowsHookEx = Fiddle::Function.new(@dll['SetWindowsHookExA'], [Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_VOIDP)
	callback = Fiddle::Closure::BlockCaller.new(Fiddle::TYPE_VOIDP, [Fiddle::TYPE_INT, Fiddle::TYPE_INTPTR_T, Fiddle::TYPE_VOIDP]) do |code, wparam, lparam|
		if lparam[0]==-92 then #left ALT, right ALT=-91
			hook_alt() if wparam==257 # on WM_KEYUP
			1 
		else
			0
		end
	end
	@hook = setWindowsHookEx.call(13, callback, nil, 0)	# WH_KEYBOARD_LL=13
	puts "ALT will be intercepted"
 end
 
 def deactivate(view)
     Fiddle::Function.new(@dll['UnhookWindowsHookEx'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT).call(@hook)
	 puts "ALT will NOT be intercepted"
 end
	  
 def hook_alt() #visual test
		if @i.inspect=="1"
			UI.set_cursor(631)
			@i=0		
		else
			UI.set_cursor(634)
			@i=1		
		end
 end
 
end

Sketchup.active_model.select_tool(Alt_key.new)
1 Like