Flags on onKeyDown?
-
The flags are (as best as I could ever tell)
1 = left mouse button down
2 = right mouse button down
4 = shift down
8 = ctrl down
16 = ?
32 = alt downThen add them together to determine if the user has more than one key down. So shift + ctrl would give a flag of 12. Alt + ctrl gives a flag of 40. All of them down together give a flag of 47 (not including 16. I can't find 16, so I can't press it).
Now, I don't know how to use that info in a script though. Sorry, I probably didn't help very much. Can you think of a good way to examine the flag 47 and decide what keys that means are being held down?
Chris
-
Well, it's a bit field. So using your information...
47.to_s(2) 101111
Each position in the binary would represent a key state.
1 - left mouse on 0 - right mouse off 1 - shift on 1 - ctrl on 1 - ? 1 - alt on
But my problem is the flag value does not change if I have the SHIFT key down or not, when I think it should. Maybe I'm making bad assumptions.
-
didn't know you could convert to binary that way. neat.
-
Oh, here's a thought. onKeyDown/Up use the "key" argument, while the others (onMouseMove, onRButtonDown, etc) use the "flag" argument. So with onkeyup, its not really a flag, but a keycode or keyID, whereas the other methods return a flag using the system I described above.
Don't know how that changes things....
-
What would be the best way to read the CTRL key in Win and its corresponding key on MAC. I am writing about a case without creating a new tool.
I would like to read a state of CTRL key when an user presses a toolbar icon.
Tomasz
-
if (flags & 4)==4
shift=true
end -
Ah, so maybe that is what these constants are for?
["CONSTRAIN_MODIFIER_KEY", "CONSTRAIN_MODIFIER_MASK", "ALT_MODIFIER_KEY", "ALT_MODIFIER_MASK", "COPY_MODIFIER_KEY", "COPY_MODIFIER_MASK"]
-
@unknownuser said:
.. I am writing about a case without creating a new tool.
@jim said:
["CONSTRAIN_MODIFIER_KEY", "CONSTRAIN_MODIFIER_MASK", "ALT_MODIFIER_KEY", "ALT_MODIFIER_MASK", "COPY_MODIFIER_KEY", "COPY_MODIFIER_MASK"]
Actually creating a tool in not a big deal, but I thought there is a way to omit that.
In SU2KT there is no need for creating the tool and all necessary methods to handle it.
I just need to read a state of CTRL\MENU key upon the start-up of the plugin. -
Can't be done, as far as I know. I agree it would be nice.
-
class SU2KT_key def SU2KT_key;;key_pressed? @key end def SU2KT_key;;activate @key=false end def SU2KT_key;;onKeyDown(key, repeat, flags, view) @key=(key==COPY_MODIFIER_KEY) puts @key end end keytool=SU2KT_key.new Sketchup.active_model.select_tool SU2KT_key sleep 1 puts SU2KT_key.key_pressed?
Why does it always give me 'false' on reload of the script? It looks that SU doesn't check the keyboard on creation of a new tool, otherwise I would get the @key value BEFORE sleep 1. Am I right?
Thanks
Tomasz -
@unknownuser said:
I just need to read a state of CTRL\MENU key upon the start-up of the plugin.
Did you try with the event
OnMouseMove
, which has aflags
argument too? -
@unknownuser said:
@unknownuser said:
I just need to read a state of CTRL\MENU key upon the start-up of the plugin.
Did you try with the event
OnMouseMove
, which has aflags
argument too?… This is a bit off-topic, but on windows
` class Flagtestdef onMouseMove(flags,x,y,view)
UI.messagebox flags
end
endSketchup.active_model.select_tool Flagtest.new`
will come up with 0 on mouse move, if no keys are pressed. on my mac, it's 256. Any idea why?update:
Also, onMouseMove only activates when the mouse moves in the model area, so it wouldn't activate on startup of the plugin. -
ok, I am using the "flags" parameter of the onLButtonUp method to determine ctrl, alt key status. This is my code so far. It works, but I have a feeling it could be optimized. I really don't manipulate strings very well. I nearly turned it all into an array.
` def onLButtonUp(flags, x, y, view)
flag_bin = flags.to_s(2)
flag_bin_to_add = 6 - flag_bin.length
zeros = "0"*flag_bin_to_add
adjusted_flag = (zeros + flag_bin).reversectrlk = true if adjusted_flag[3,1] == "1"
altk = true if adjusted_flag[5,1] == "1"`So I take the flag, turn it into binary as Jim showed. But a value less than 32 makes a binary with less than 6 digits in it. So if the user is holding alt, then a 6 digit binary is given. If they are holding ctrl, then only a 4 digit binary is given. So I am adding zeros to the binary string, then reversing it because I like the binary to be ordered according to the numerical representations for each key, lowest number key on the left, highest number key on the right.
Then I set my variables to be true if their binary digit is true, indicating that their key was pressed.
I hope that makes sense. Again, this seems to work great. But it feels klunky. Is there an easier way to parse the bit field?
Any optimization ideas?
Chris
ps - I tacked it onto this thread since this thread is what pointed me in the direction of how to work with the "flags" parameter.
-
shift = (flags & CONSTRAIN_MODIFIER_MASK) == CONSTRAIN_MODIFIER_MASK
Use the
&
for bitwise comparison. -
I don't quite understand what that means. What is bitwise?
-
-
Nice little list of Ruby's operators http://www.tutorialspoint.com/ruby/ruby_operators.htm
-
@chris fullmer said:
flag_bin = flags.to_s(2) flag_bin_to_add = 6 - flag_bin.length zeros = "0"*flag_bin_to_add adjusted_flag = (zeros + flag_bin).reverse
Stick in a non-zero bit to the left of the flags.
Quickly:
(flags | 128).to_s(2)[2..7].reverse
Slowly:
flags # 00bbbbbb flags | 128 # 10bbbbbb (flags | 128).to_s(2) # 8-char string version (flags | 128).to_s(2)[2..7] # the 6 chars your really want
-
Converting to string is very inefficient.
-
@chris fullmer said:
I don't quite understand what that means. What is bitwise?
Think in binary, then compare them column for column. It's a logical, bit-to-bit comparison.
5 in binary is 101 6 in binary is 110 5 & 6 is; logical AND each column (bit position) Columns ________________ | 1 | 0 | 1 <- decimal 5 | and | and | and | 1 | 1 | 0 <- decimal 6 ================= 1 0 0 so 101 & 110 = 100 <- decimal 4
The masks are constants, which are set to some convenient values:
# I just made these up 001 = CONSTRAIN_MASK 010 = ALT_MASK 100 = COPY_MASK
So if the flag is decimal value 7:
111 <- decimal 7
In order to check the CONSTRAIN_MASK, you logical AND the CONSTRAIN_MASK with the flag value:
111 <- flag value decimal 7 & 001 <- contain mask constant ====== 001 <- CONSTRAIN_MASK set true or in real ruby as Thomas wrote; contrain_key_down = ((flags & CONSTRAIN_MODIFIER_MASK) == CONSTRAIN_MODIFIER_MASK) copy_key_down = ((flags & COPY_MODIFIER_MASK) == COPY_MODIFIER_MASK) alt_key_down = ((flags & ALT_MODIFIER_MASK) == ALT_MODIFIER_MASK)
Although Adam B might know a clever way to decode them all in one elegant statement.
Advertisement