Hot key sends data to HTML dialog

I’m fairly proficient at setting up and utilizing HTML dialogs. However, I’m not sure how to have my tool collect input from a hot key (ie. Ctrl-key) which would then trigger my HTML dialog to then run some javascript in my HTML menu.

I know this is a clear as mud.

Basically if the user click the Ctrl-key repeatedly it will not only change my variable but also trigger the HTML dialog code so that it will send the updated variable to my HTML menu.

When the hot key is entered while the SketchUp viewport is focused, you need a UI::Tool. Then the tool’s handler function does something, either on the Ruby side or by sending a message to the dialog (dialog.execute_script), or both.

It may be worth asking why pressing Ctrl in the SU window should affect a dialog window. It may not be the most logical connection for the user.

1 Like

If you do not really have an active tool, then you must create a menu item, and the user must assign a keychord accelerator to it.

1 Like

I guess I should preface this question with a little more background:

This is for my new blocking tool in the Wall plugin. Normally the user enters in a rotation angle (0-360 deg.) in the html menu to rotate the block about its axis.

However this is a slow and cumbersome way in which to quickly toggle the orientation of the block from a zero rotation to a ninety degree rotation.

I would like to add hotkey to toggle the rotation angle, this part is not hard.

My question is how to signal this change in the HTML menu so it update accordingly once the hot key is pressed.

1 Like

Well, do not update your html based on the fact that a user did something (like clicking or entering a button), but update your ui based on the fact that a state (the rotation) has changed.

4 Likes

That is exactly what I want to do but I don’t know how to do that.

There is not really a single good answer on how to do this, since it really depends on the architecture your app uses. Your app will on some point have a state that gets updated by various events, and, in return will update other things that bind on the events your state emits.

Let say an action changes the rotation of an entity. That event can be captured by an observer and can be programmed that it will update your app state. Other parts of your app could listen to state changes and get updates whenever (a part of) the state changes. A simple starting point is using the observer pattern (google: observer pattern in ruby). There are also a few cons against that pattern… but then again, every pattern is an antipattern.

2 Likes

In my onKeyDown method I have this block of code:

if( key == VK_SHIFT && rpt == 1 )
		if @Rotposition == 1
			@Rotposition = 2
			@Block_rot = 90.0
		elsif @Blockposition == 2
			@Rotposition = 1
			@Block_rot = 0.0
		else
			# UI.messagebox "Invalid position"
			@Rotposition = 1
			@Block_rot = 0.0	
		end
	end

When this block of code is triggered by the shift key I need to somehow send that updated block rotation to the HTML dialog via a callback so that it executes a javascript function in my HTML.

What is the primary intention of this block? Update an entity? Or, updating a ui and then let the user continue from there?

Just experimenting a bit and tried this:

if( key == VK_SHIFT && rpt == 1 )
		if @Rotposition == 1
			@Rotposition = 2
			@Block_rot = 90.0
		elsif @Blockposition == 2
			@Rotposition = 1
			@Block_rot = 0.0
		else
			# UI.messagebox "Invalid Light Position."
			@Rotposition = 1
			@Block_rot = 0.0	
		end

		@Block_rot = @Block_rot.round(4)

		js_command = 'pushdatarot( "' + @Block_rot.to_s + '|' + @Licensemode.to_s + '" );'
		@dlg03.execute_script(js_command)	
	end

What do you know it works!

When I trigger this section of code via the shift hokey it then also send the execute command to the javascript. I didn’t realize I could setup this method (execute_script) anywhere in my code, sometimes you just need to take the blinders off.

This will work, for now :slight_smile: But maintaining this is hard. Maybe after a while you do not only want to update that dialog, but also another one, or something else, or the function in your dialog will change by name or the number of arguments. Then you would need to go back and edit your tool to also update those, and any other tool that does this.

I cannot force you of course to spend a little more time in solving this upfront, but I can guarantee you that in the long run, upfront investments will keep you from maintenance headaches.

2 Likes

Also be aware that the onKeyDown does not fire for all keys on both platforms. (It’s safer to use onKeyUp.)

1 Like

Or stay away from keys that are used on a system level or have hardcoded uses in SketchUp (Delete, Tab, Win? …).

1 Like