flags parameter in a tool's onKeyDown/Up

flags parameter in a tool's onKeyDown/Up

Postby ittayd » Fri Jun 09, 2017 2:51 pm

What is the meaning of the values of the flags parameter to the key callback methods in a tool? I understand they are supposed to show what modifier keys are pressed, but their value in the callback methods is confusing to me.

It is worth mentioning that shift has the value of 4 (100 in binary, or the 3rd bit) and ctrl is 8 (1000 in binary, or the 4th bit)

Adding `puts '%32b' % flags` to both onKeyDown and onKeyUp, I see the following:
Hitting ctrl:
onKeyDown prints 11101 (meaning, so it seems, that both ctrl and shift are pressed)
onKeyUp prints 1100000000011101.
Hitting shift:
onKeyDown prints 101010 (ctrl is pressed)
onKeyUp prints 1100000000101010
Hitting ctrl+shift as ctrl, keeping pressed and then shift:
onKeyDown prints 11101 on hitting ctrl, note again the flags mean both ctrl and shift are down
onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the shift is down.
Similarly when releasing the keys, the values are for shift and ctrl only

I'm trying to make sense of it instead of keeping my own state of when ctrl and shift are pressed and released.
I couldn't find code examples that use flags in onKeyDown and onKeyUp either
0
Last edited by ittayd on Mon Jun 12, 2017 8:36 pm, edited 2 times in total.

ittayd 
 

Re: flags parameter in a tool's onKeyDown/Up

Postby Dan Rathbun » Sun Jun 11, 2017 10:17 pm

ittayd wrote:What is the meaning of the values of the flags parameter to the key callback methods in a tool?

Each bit in a bitfield represents a boolean switch for some feature.

In this case, is it whether a certain key's flag is sent to an onKey... callback.

To test whether whatever virtual keycode (that you got in the key argument,) is also associated with one of the "modifier" keys (or mouse buttons,) you test the flags bitfield against the desired bitmask constant (listed at the top of the Tools class documentation page,) using the bitwise AND (&) method.

If the result is 0 then that modifier key was not associated with the key.
If the result is non-0 (usually the value of the mask constant,) then that modifier key WAS associated with the key.

This is accomplished because the & operation ignores all bits in the bitfield that are unset (0) in the mask argument. (Only that bit that you are testing for is set to 1 in the mask.)

See wikipedia:
http://en.wikipedia.org/wiki/Mask_(computing)#Querying_the_status_of_a_bit



Was the SHIFT modifier associated with the key argument ?
flags & CONSTRAIN_MODIFIER_MASK != 0

Was the Alt/Option on Mac, CTRL on PC modifier associated with the key argument ?
flags & COPY_MODIFIER_MASK != 0

Was the Command on Mac, ALT on PC modifier associated with the key argument ?
flags & ALT_MODIFIER_MASK != 0

The mousekeys modifier constants are:
MK_ALT, MK_COMMAND, MK_CONTROL, MK_LBUTTON, MK_MBUTTON, MK_RBUTTON, MK_SHIFT

It may help you to wrap them up into methods with names that make sense to you:
ie:
Code: Select all
def mask_has_shift?(flags)
  flags & CONSTRAIN_MODIFIER_MASK != 0
end

... etc.

BTW, all the virtual key codes and modifier mask constants are global (the API defines them at the toplevel.)

:ugeek:
0
Last edited by Dan Rathbun on Mon Jun 12, 2017 6:33 pm, edited 2 times in total.
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: flags parameter in a tool's onKeyDown/Up

    Postby ittayd » Mon Jun 12, 2017 8:04 am

    Dan Rathbun wrote:Each bit in a bitfield represents a boolean switch for some feature.

    In this case, is it whether a certain key's flag is sent to an onKey... callback.



    I'm aware of what the flags are supposed to be and how to do bit manipulation, but the values make no sense. E.g. on ctrl then shift, the callback for the shift press doesn't pass on flags that show that ctrl is pressed
    0

    ittayd 
     

    Re: flags parameter in a tool's onKeyDown/Up

    Postby Dan Rathbun » Mon Jun 12, 2017 6:31 pm

    You clearly asked the following question which I answered (not just for your benefit, but also for others who may be new to programming and/or the SKetchUp Ruby API.)

    ittayd wrote:What is the meaning of the values of the flags parameter to the key callback methods in a tool?

    I'm editing my previous post to insert this question, to make it clear what I am answering.



    Re-reading your original post the second paragraph is not at all clear. Ie: Run-on sentences. Not stating what callback (onKeyUp/onKeyDown) you are referencing.

    In order for us to see what you see, we'd need a code snippet to try to re-create the issue. (Meaning, that if you've already written the code to test this, share it. Don't make us write up code also.)

    This API has been working for more than 10 years. There is no clamour from the developers at large that something with this is amiss.

    And BTW, you do not state what SketchUp version you are testing.
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: flags parameter in a tool's onKeyDown/Up

      Postby ittayd » Mon Jun 12, 2017 7:00 pm

      Dan Rathbun wrote:Re-reading your original post the second paragraph is not at all clear. Ie: Run-on sentences. Not stating what callback (onKeyUp/onKeyDown) you are referencing.


      Sorry if my message wasn't clear, English is not my native language (I don't even know what run-on sentences mean) and I wrote the post after a couple of hours of frustration. I edited it to make it clearer

      I couldn't find code that demonstrated the use of flags in onKeyDown or onKeyUp (including searching my plugins folder, github, google).
      0

      ittayd 
       

      Re: flags parameter in a tool's onKeyDown/Up

      Postby Dan Rathbun » Mon Jun 12, 2017 7:35 pm

      ittayd wrote:onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the ctrl is down.

      Why do you say this, when it is clear that the 4th bit (for CTRL modifier) is set ?

      ittayd wrote:I couldn't find code examples that use flags in onKeyDown and onKeyUp either

      The API docs have junky examples. They've never been a good teaching tool.
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: flags parameter in a tool's onKeyDown/Up

        Postby ittayd » Mon Jun 12, 2017 8:37 pm

        Dan Rathbun wrote:
        ittayd wrote:onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the ctrl is down.

        Why do you say this, when it is clear that the 4th bit (for CTRL modifier) is set ?


        Typo. ctrl should be shift. But it is pretty obvious from the other examples (as well as this one) that the flags values are probably the bit set they were meant to be
        0

        ittayd 
         

        Re: flags parameter in a tool's onKeyDown/Up

        Postby Dan Rathbun » Mon Jun 12, 2017 9:49 pm

        ittayd wrote: Typo. ctrl should be shift.

        ... okay, then, let us try again:

        ittayd wrote:
        Hitting ctrl+shift as ctrl, keeping pressed and then shift:
        onKeyDown prints 11101 on hitting ctrl, note again the flags mean both ctrl and shift are down
        onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the shift is down.

        It seems you are making a fundamental mistake. In this example in the quote above, the detected KEY is the SHIFT key, and it is what is causing the onKeyDown() callback to fire, and it's keycode VK_SHIFT will be stored in the key argument.

        So, then you do not detect IT within the flags bitfield, because you already know what the firing key is.

        Instead, at this point in time, you check the flags bitfield for whatever other modifiers are associated with this press of the SHIFT key.

        Here's an example, assuming you've defined boolean methods to test the modifiers (as I explained in my educational post above.)

        Code: Select all
        case key
        when VK_SHIFT
          if flags_has_copy_modifier?
            # CTRL is also pressed, act accordingly
          elsif flags_has_alt_modifier?
            # ALT is also pressed, act accordingly
          else
            # act is some other way
          end
        when VK_CONTROL
          if flags_has_constrain_modifier?
            # SHIFT is also pressed, act accordingly
          elsif flags_has_alt_modifier?
            # ALT is also pressed, act accordingly
          else
            # act is some other way
          end
        else
          if flags_has_constrain_modifier?
            # SHIFT is also pressed, act accordingly
          elsif flags_has_copy_modifier?
            # CTRL is also pressed, act accordingly
          elsif flags_has_alt_modifier?
            # ALT is also pressed, act accordingly
          else
            # act is some other way
          end
        end
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: flags parameter in a tool's onKeyDown/Up

          Postby ittayd » Tue Jun 13, 2017 8:41 am

          Dan Rathbun wrote:
          ittayd wrote:
          Hitting ctrl+shift as ctrl, keeping pressed and then shift:
          onKeyDown prints 11101 on hitting ctrl, note again the flags mean both ctrl and shift are down
          onKeyDown prints 101010 on hitting shift (when the ctrl is still down) which suggests it "forgets" the shift is down.

          It seems you are making a fundamental mistake. In this example in the quote above, the detected KEY is the SHIFT key, and it is what is causing the onKeyDown() callback to fire, and it's keycode VK_SHIFT will be stored in the key argument.

          So, then you do not detect IT within the flags bitfield, because you already know what the firing key is.

          Instead, at this point in time, you check the flags bitfield for whatever other modifiers are associated with this press of the SHIFT key.


          OK, so you took just the last line of a whole example:
          1. When I press'shift' only, without 'ctrl' first, I get 101010. As if ctrl is pressed, but it is not.
          2. And even with the 'ctrl' pressed first, what is the meaning of the 6th and 2nd bits?
          3. When I press 'ctrl' only, without shift, I get 11101, as if ctrl and shift are pressed.
          4. And also, what is the meaning of the 5th and 1st bits?
          5. I get the same codes in onKeyDown and onKeyUp regardless of whether I press ctrl with shift already pressed or not or when I press shift and ctrl is pressed before or not.

          And sure, the 'key' parameter does hold the code for ctrl and shift, and I ended up maintaining an instance variable to flip bits on and off when the callbacks are called. But I'm still confused as to the meaning of the flags parameter.

          I'd like to mention that I have 16 years of professional programming: 1.5 years of Ruby, 8 years Java, 2 years of Scala, 5 years of C/C++ and in particular 1 year of Linux kernel development (in the virtual memory handling subsystem), as well as other assorted languages. In particular, I had many chances of doing bit manipulation of variables as well as work with bit sets.

          All of that doesn't help me in understand the values of the flags parameter which in the case of onKeyDown/onKeyUp doesn't make sense to me. For comparison, in onLButtonDown, the flags are 1001 if ctrl is down when the button is clicked, 101 if shift is down and 1101 if both and just 1 when nothing is pressed. These are consistent with showing the value of the modifier keys, but inconsistent with the values I listed in my original post.
          0

          ittayd 
           

          Re: flags parameter in a tool's onKeyDown/Up

          Postby fredo6 » Wed Jun 14, 2017 11:37 pm

          This is what I use in my plugins


          def buttonLDown?(flags) ; flags & MK_LBUTTON == MK_LBUTTON ; end
          def shiftDown?(flags) ; flags & CONSTRAIN_MODIFIER_MASK == CONSTRAIN_MODIFIER_MASK ; end
          def ctrlDown?(flags) ; flags & COPY_MODIFIER_MASK == COPY_MODIFIER_MASK ; end
          def altDown?(flags) ; flags & ALT_MODIFIER_MASK == ALT_MODIFIER_MASK ; end
          0
          User avatar
          fredo6 
          PluginStore Author
          PluginStore Author
           

          Re: flags parameter in a tool's onKeyDown/Up

          Postby Dan Rathbun » Thu Jun 15, 2017 10:04 pm

          ittayd wrote:All of that doesn't help me in understand the values of the flags parameter which in the case of onKeyDown/onKeyUp doesn't make sense to me.

          You know, ... I've been involved with SKetchUp Ruby for like 10 years, and the docs are still very poor. Also there has never been a public API BugTracker, so after all this time I have lost track of all the bugs, which ones have been fixed, and which ones haven't.

          But this thread has jogged my memory a bit. I seem to remember an old thread here, (perhaps started by Jim Foltz, in which we discussed funky issues with the key callbacks in the abstract Tool class. I think he even stick a test script in it named "KeyTest.rb" ? I think in it we all agreed that onKeyDown had bugs, and you had to rely more on onKeyUp.

          I did a bit of testing (SU2016), and I do see that the repeat arg still is not correct in onKeyDown.

          I'll see if I can find that old thread.
          0
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: flags parameter in a tool's onKeyDown/Up

            Postby Dan Rathbun » Thu Jun 15, 2017 10:26 pm

            Ah okay, 3 years and two months ago, Fredo was noticing these same issues in the following topic thread:

            onKeyDown: getting the charcater value from key

            In that thread, post # 9, I noted several other related topic threads, and the one that I just remembered by Jim Foltz, which was back in 2009.
            flags on onKeyDown?
            (Note that at this time Chris Fullmer was not yet a member of the SketchUp Team, and had not yet learned much programming.)

            See other related threads:

            My Apple Keyboard VK_KEYS

            very difficult key codes!

            keycodes intelmac
            * Jim's KeyTool.rb is also posted in this topic thread, but not sure if it is the latest version ?

            Key sequence in ruby



            Now, @Anton_S posted a Windows only method that calls Windows API C functions, in this post:
            http://sketchucation.com/forums/viewtop ... st#p522915

            :ugeek:
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              SketchUcation One-Liner Adverts

              by Ad Machine » 5 minutes ago



              Ad Machine 
              Robot
               



               

              Return to Developers' Forum

              Who is online

              Users browsing this forum: Bing [Bot] and 4 guests

              Visit our sponsors: