How to assign keyboard buttons within a script

I want to make a very simple Ruby Script to help with my workflow and seeing if any scripters out there can give me some advice.

So, all I want from the script is to draw a line based on pressing one of the cursor keys then inputting the length (and pressing enter).

Up Cursor = Draws a line along the green axis (positive value)
Down Cursor = Draws a line along the green axis (negative value)
RIght Cursor = Draws a line along the Red axis (Positive value)
Left Cursor = Draws a line along the Red axis (Negative value)

For example, to draw a square which is 1000mm x 1000mm:

Press ‘Up’ Cursor, then type 1000, press enter
Press ‘Right’ Cursor, then type 1000, press enter
Press ‘Down’ Cursor, then type 1000, press enter
Press 'Left; Cursor, then type 1000, press enter

I know how to create a line in Ruby and also to create variables / objects but not sure how to assign the cursor keys to carry out the commands.

Also I will need some kind of button to ‘switch off’ the script when I don’t need it so the cursor keys can go back to their regular day jobs…

Ay help appreciated :slight_smile:

The arrow keys are used by SketchUp for axis inference lock.
It is not a good idea to reassign them as a shortcut (manually) to fire a plugin command.

FYI, assigning keyboard shortcuts is considered an end user function and is not exposed to the API.


Now that said, something like you describe might be possible by writing a custom Ruby Tool.
But again, it would violate the conventional use of the arrow keys.

Anyway, in a tool you trap keypresses with the onKeyDown and onKeyUp callback methods.
You compare the key code with the global virtual key constants, they are prefixed with VK_, see:

Look at the Tool class (really a protocol not a class). You want to code a tool that handles the appropriate onXXX event callbacks in the Tool API .

Don’t think script … this is linear programming.

A SketchUp tool is an event driven code object. The tool is activated and the object has callback methods that react to events (key presses, mouse moves, user input into the VCB, interruptions by view manipulators, etc.)
Tools are not switched “off”, they are deactivated by another tool being activated.

Please keep in mind that SketchUp is not a command driven interface like AutoCAD.
It has been purposefully designed to be a tool driven interface.

Thanks for the answers.
As usual I don’t understand a lot of this but am trying to figure it out.
So let’s say I wanted to create the custom tool as suggested, I don’t mind using shift+arrow keys if that avoids any violations.
Is there a place on this forum where I can offer this job to a developer to write this for me? (Then I can learn from looking at the code they used). I can pay for that.

Have you looked at the example tool I just recently posted (6 days ago) in this forum ?
It traps SHIFT and CTRL modifiers to mouse button clicks in order to add or subtract from a selection set.

Use version 2 …
[Example] A simple FaceSniffer tool extension - #2 by DanRathbun

In your case instead of looking for mouse clicks in onLButtonUp, you’ll have case clauses in onKeyUp that test for VK_UP, VK_DOWN, etc., to set set an internal instance variable that would hold a vector along which you’ll draw an edge.
For example …

case key
when VK_UP
  @vec = Y_AXIS
when VK_DOWN
  @vec = Y_AXIS.reverse
when VK_RIGHT
  @vec = X_AXIS
when VK_LEFT
  @vec = X_AXIS.reverse
else
  # ignore key ?
end

And you’ll use the onUserText callback to receive user input from the VCB (aka Measurements input control. “VCB” is an acronym for Value Control Box.)

But to begin with, the tool’s initial state must get an Inputpoint from the user to know where to begin to draw from.

Basically your idea is a variation on a standard Edge drawing tool (ie, “LineTool”).
Trimble has posted an example and tutorial Line Tool. (The tutorials have verbose comments. The examples have little or no commentary, but the code is the same.)

If you do not know how to use GitHub to create a fork etc., then you can simply copy the two files and paste them into your code editor. At GitHub, display the files (each in turn) and on the file toolbar at the right, between the edit (pencil) button and the delete (trash can) button, is the copy button (one page overlapping another.) Use the copy button to copy each file to the clipboard memory and then switch to your code editor and paste it in (CTRL+V).

Make sure to keep the same file and folder structure. The extension registrar file is at the top level (eventually in the “Plugins” folder,) and the “main.rb” file goes into a subfolder that must be the same name as the registrar file.

You can rename the folder and registrar file to some thing like “nickcrush_linetool.rb” and the folder to "“nickcrush_linetool”, but also change the top module to “NickCrush” and the submodule to “LineTool” in each file. Then, in the registrar file, edit the fields of the SketchupExtension object and the comments at the top to reflect your modifications.


Usually we ask such postings be posted in the Commerical and Collaborative Work category.

I am not interested as the feature your talking about is already available in the native Line Tool.

After activating the tool, simply click a point in the model, then one of the LEFT or RIGHT arrow keys to lock an axis inference, then type a value and hit ENTER. The pencil cursor will remain at the start point, so the direction along either axis locked will be controlled by either a positive of negative value. Positive away from the start point, negative towards the start point.

1 Like

Thanks for that Dan, it all looks really helpful and relevant to get me started, hopefully I should be abke to figure it out!

1 Like

Look at the Tool class (really a protocol not a class). You want to code a tool that handles the appropriate onXXX event callbacks in the Tool API .

Are you able to impart a bit more detail on how to do this? If it’s not too much trouble, could I ask if you could provide the code for just one of the key functions so I can copy / amend it for the rest? (it doesn’t have to be one of the cursor arrows if this is problematic - any key would do) - so for example, just by pressing creates a line positively along the red axis?

Thanks