The Enter Key and Draw Tools

In order to terminate a certain operation I am trying to allow the user to click the “Enter” key or return key in my draw tool.

My basic code is simply:

if( key == VK_RETURN && rpt == 1 )

For some reason I can’t get this to work with SketchUp running on Windows. Has anyone encountered this same problem?

VK_RETURN does not seem to exist as a constant. You can always use puts key.to_s to get the key code. Return is 13 on Windows, so:

 if( key == 13 && rpt == 1 ) 

should work.

1 Like

Does anyone know if key == 13 will also work on Mac?

This does seem to work flawlessly on Windows. The Enter key is a much better option for terminating an operation than the down arrow key, my only concern is that this will not work on a Mac.

I would be really curious to know if the correct key code for the Mac enter key is 36.

I guess in my draw tool I should probably check if the user is on a Mac or Windows machine and then adjust my logic accordingly.

Yes, that is also the code for enter/return on Mac.

1 Like

As well as the working Tool key-press/release 13 - there is a separate Tool method to catch the user’s ‘enter’ presses onReturn
http://ruby.sketchup.com/Sketchup/Tool.html#onReturn-instance_method

2 Likes

Something like this will probably work:

if WIN_OS
		if( key == 13 && rpt == 1  ) || ( key == VK_DOWN && rpt == 1  )
			if  @pointcount >= 2
				@state = STATE_PICK_FINAL
				self.update_state
			end
		end
	else
		if( key == 36 && rpt == 1  ) || ( key == VK_DOWN && rpt == 1  )
			if  @pointcount >= 2
				@state = STATE_PICK_FINAL
				self.update_state
			end
		end
	end

I’ll keep the down arrow for now since a few users are probably in the habit of using the down arrow and probably better not to remove it just yet.

The arrows keys should only be used for inferencing IMO. Teaching the users to use them for other tasks is bad and will lead to confusion later on.

1 Like

I agree, but unfortunately I’ve already documented in my tutorial videos to use the down arrow. Eventually I will remove this termination option but for a few cycles I will probably keep it as it is, until the user base is comfortable with using the enter key.

You can e.g. can define your own enter key, right after you have WIN_OS something like this:

MY_VK_RETURN ||= WIN_OS ? 13 : 36

then later on it will be “simplified”:

.

if( key == MY_VK_RETURN  && rpt == 1  ) || ( key == VK_DOWN && rpt == 1  )
  if  @pointcount >= 2
    @state = STATE_PICK_FINAL
    self.update_state
  end
end

Note that this wont work for any tool using the VCB, where Enter is used to enter a value. Also no native tool uses Enter to go to the next step so there may be more polished ways to achieve this.

2 Likes

I started with the down arrow but everyone wanted the Enter key, what would you suggest.

This tool allows the user to select a number of points which define a stemwall foundation, after they’ve picked their points I need to have a key which they can select (click) to terminate their point selection and proceed to generate the foundation.

The return/enter key seems to be the natural selection or the key one would expect to click, but perhaps there is a better choice. SU is a little bit funny that way, I’m still not really used to clicking the space bar to terminate actions, the ESC key seems like the better choice.

Maybe this…
Tool.html#onReturn-instance_method
However this one does not heve a parameter: repeat

I’m thinking more in the direction of not having keys at all for going to the next step, but having the tool do it for you. Native rectangle first lets you click a start corner, then a second corner, then a third and you are done. This wont work though with tools where there is a variable amount of things to pick/select in each step. These tool are a bit tricker to design.

If a tool has just two stages for picking objects, e.g. like a Solid Trim tool, then selecting the first object to be trimmed could just take you directly into the second stage of selecting trimmers. If you want more than one trimmer you keep click objects. If you want to trim more than one object you use a preselection (which should be mentioned in the Instructor and status text). This approach doesn’t scale to tools which more than two stages though.

Not sure if this can be applied in your tool, but it could maybe be useful in the future.

1 Like

This tool has in indeterminate amount of points that may be selected (min. of two points).

You are correct that the Enter key doesn’t really work for me with this tool since I do use the VCB and allow the user to key in a length for the next segment if they so choose.

I just tested it and when I key in a length and hit the Enter key it terminates as expected and rather than move on to the next point/segment it jumps out of the tool and draws the foundation. So I guess trying to use the Enter key is fundamentally flawed due to the use of the VCB.

I guess I’m back to using the down arrow.

I’m wondering if I could use the “End” key? Would this also work on a Mac? Does SketchUp use the End key for something else?

The key code for the End key is 35 on Windows, and appears to be 119 on Mac.

I’ve replaced the previous block of code with this instead:

if WIN_OS
		if( key == 35 && rpt == 1  ) || ( key == VK_DOWN && rpt == 1  )
			if  @pointcount >= 2
				@state = STATE_PICK_FINAL
				self.update_state
			end
		end
	else
		if( key == 119 && rpt == 1  ) || ( key == VK_DOWN && rpt == 1  )
			if  @pointcount >= 2
				@state = STATE_PICK_FINAL
				self.update_state
			end
		end
	end

It works great in Windows but I need to check if it will work on a Mac.

I think End is a small little bottom far away on the keyboard, that some users doesn’t even seem aware of. Also I don’t think all keyboards has an End button but sometimes combines function keys and arrow keys for it. I’d rather use Tab which is larger, more consistency placed and already used in a quite similar fashion for jumping between input fields. Tab would still won’t feel like a native SketchUp tool but could get the job done.

1 Like

You are probably right, everyone has a Tab key and its an easy reach on the keyboard. However, the End key syntactically is fitting since the user is choosing to end the point selection.

if Enter/Return follows a key :onUserText(text, view) is called…

if Enter/Return follows a click :onReturn(view) is called…

conditionals in :onReturn(view) dictate what happens next…

why use other keys when Enter/Return is appropriate…

I don’t even know if mac has an ‘End’ key and ‘Tab’ is, by default, set to accessibility control by os x…

john

1 Like

On a general note, I have a couple of scripts with the same approach, the user needs to indicate that a selection is complete. I’ve found that tab (key == 9) is a good choice due to its placement on the keyboard in the upper left area. The right hand can stay on the mouse.

1 Like