I've just written the code and it seems to work nicely.
External javascript file (loaded by all web dialogs:
function port_key(){
//Sends the keycode from the event to a Ruby callback.
//This can be used for web dialogs inside a custom tools to send key events to the tool to change its behavior even when the web dialog is focused.
//Run "document.onkeyup=port_key;" to initialize. onkeyup is used to avoid overriding calls to onkeydown already in use in the dialog. onkeypress doesn't fire for modifier keys.
e = window.event || e;
keycode = e.keyCode || e.which
//It might be wise to only proceed for certain keys here, e.g. modifier keys or whatever keys are used in the tool.
window.location='skp;port_key@' + keycode;
}
In Dialog.show:
js = "document.onkeyup=port_key;"
@dialog.execute_script js
Dialog callback:
#Port key event to tool
@dialog.add_action_callback("port_key") { |_, callbacks|
self.onKeyDown callbacks.to_i, false, 0, Sketchup.active_model.active_view#This tool doesn't use flags so it can just be set to 0
}
I haven't really bothered locking into the flags since my tools doesn't use them, just tab, alt, and arrow keys.
Here's a list of the constants representing key codes in SU and here's the key codes from the javascript events in different browser.
It's only tested on windows but the key codes seem to match quite well for most of the keys. My code might have some Mac bugs though, idk since I can't test it.