Flags on onKeyDown?
-
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