Key sequence in ruby
-
How do I use a sequence of keys in ruby?
I've tried with:
if (key == 18 && key == 38) # Alt and Up Arrow
and:
if (ALT_MODIFIER_KEY && key == 38)
and:
if (key == ALT_MODIFIER_KEY && key == 38) -
In a tool, when you define onKeyDown, it has a bunch of parameters:
def onKeyDown(key, repeat, flags, view)The "flags" parameter will be a numerical value representing various events or combinations of events. The values are bit-coded and can be added for combined events:
1 - Left MB
2 - Right MB
4 - SHIFT key
8 - CTRL key
16 - ???
32 - ALT keySo, CTRL+SHIFT+ALT would be a flag value of 44 (8+4+32)
In your example, then, you want to check if flags==32 and key==38
-
Thanks for your help, but it still won't work for me.
Here is what I've done (a bit shorter than the real code):def onKeyDown(key, repeat, flags, view) if (flags == 32 && key == 38) # Alt + Up dist = Geom;;Point3d.new [0, 0, 100]; end #if # Now move it tr = Geom;;Transformation.new (dist); Sketchup.active_model.entities.transform_entities(tr, e); end # onKeyDown -
In my nudge script, I check for either the Shift Key or the Control key. I use the onKeyDown and onKeyUp methods to test for either key. When either key is pressed, I set a corresponding true toggle for that key. When it's raised, I set the toggle back to false.
Finally, when I'm looking for the arrow keys, I test for true for either toggle and adjust the move distance accordingly. For instance, SHIFT+any arrow key is a 10X movement in distance.
Todd
-
Thanks I'll try that.
-
Yeah, in the onKeyDown method, the keys are not OR'ed.
You'll figure it out. Again, I recommend adding a "puts" statement as the first instruction in your methods to show the key code and you'll what see gets passed when.
Todd
-
YES! That worked!!!!
Thanks a lot.
As a side note. Do you have the keycodes for Mac?
On my laptop (Windows) the keycodes are:
up = 38
left = 37
right = 39
down = 40
alt = 18
shift = 16
ctrl = 17 -
Ignore any logic you see - this is from an old copy of a script. They keycodes are good though, for both Panther and Tiger, and I suspect they will be the same for Leopard too.
BTW, in OS X "Tiger" 10.4.10, the RUBY_PLATFORM constant is "fat-darwin8.8.0". If you check for "darwin" that should be good enough, thru Tiger anyways.
Todd
if !defined? KNUDGEOK then if RUBY_PLATFORM == "i386-mswin32" then # Win XP KALXMINUS = 37 # Arrow Left Key KARXPLUS = 39 # Arrow Right Key KAUYPLUS = 38 # Arrow Up Key KADYMINUS = 40 # Arrow Down Key KLTZDOWN = 188 # (comma) Less Than Key KGTZUP = 190 # (period) Greater Than Key KSHIFT = 16 # Shift Key KCONTROL = 17 # Control Key KNUDGEOK = true ; elsif RUBY_PLATFORM == "powerpc-darwin" then # Mac OSX Panther KALXMINUS = 63234 # Arrow Left Key KARXPLUS = 63235 # Arrow Right Key KAUYPLUS = 63232 # Arrow Up Key KADYMINUS = 63233 # Arrow Down Key KLTZDOWN = 44 # (comma) Less Than Key KGTZUP = 46 # (period) Greater Than Key KSHIFT = 131072 # Shift Key KCONTROL = 262144 # Control Key KNUDGEOK = true ; else KPLATFORM = "?" end end ; -
Again, thanks.
The only one missing is the Alt key. -
Oops.
Command = 1048576
Alt = 524288
Control = 262144The Command key, also known as the "apple" key, shown as a ⌘ in the Doc, is the Mac equivalent to the PC's CONTROL key. The control key on a Mac isn't used that often.
The ALT key is also named the Option key.
Todd
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement