Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Getting the State of Modifier Keys (ctrl/cmd, shift, alt)

    General Talk
    python
    3
    5
    1.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • dskeithbuckD
      dskeithbuck
      last edited by r_gigante

      This is a snippet I like to use for easily accessing the state of modifier keys when a user runs a script. I figured I would post it here as I couldn't easily track it down when I needed it today and thought it might be useful to other folks as well.

      def get_modifiers():
          """Returns state of modifier keys: ctrl, shift, alt
          
          Example Usage:
          ctrl, shift, alt = get_modifiers()
          """
      
          bc = c4d.BaseContainer()
          if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD,c4d.BFM_INPUT_CHANNEL,bc):
              ctrl = bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QCTRL
              shift = bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QSHIFT
              alt = bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QALT
              
          return ctrl, shift, alt
      
      1 Reply Last reply Reply Quote 1
      • C4DSC
        C4DS
        last edited by

        Not sure if I am right, but I think something changed in R20 (at least for C++).
        These are 2 code snippets of the same functionality in one of my plugins, which had to be changed in R20 to make it work as expected. Again ... not sure if this applies to Python.

        pre-R20

        	BaseContainer channels;
        	GetInputState(BFM_INPUT_KEYBOARD, BFM_INPUT_CHANNEL, channels);
        	Bool shiftModifier = channels.GetInt32(BFM_INPUT_QUALIFIER) & QSHIFT;
        	Bool ctrlModifier = channels.GetInt32(BFM_INPUT_QUALIFIER) & QCTRL;
        

        R20

        	BaseContainer channels;
        	GetInputState(BFM_INPUT_KEYBOARD, BFM_INPUT_CHANNEL, channels);
        	Bool shiftModifier = (channels.GetInt32(BFM_INPUT_QUALIFIER) & QSHIFT) != 0;
        	Bool ctrlModifier = (channels.GetInt32(BFM_INPUT_QUALIFIER) & QCTRL) != 0;
        
        1 Reply Last reply Reply Quote 0
        • ManuelM
          Manuel
          last edited by Manuel

          hello,

          I've moved this topic to this category.

          Thanks for sharing your snippet.

          I will probably go this way to have Boolean and not "value". Same in c++ check != 0 i will go with == QCTRL ans so on.

                  ctrl = bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QCTRL == c4d.QCTRL
                  shift = bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QSHIFT == c4d.QSHIFT
                  alt = bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QALT == c4d.QALT
          

          Regarding the pre-R20 and r20 code i don't have any idea instead that storing bool in a bool (0 or 1) make more sense.
          In your code pre-R20 you were sending value > 1 to a bool

          Cheers
          Manuel

          MAXON SDK Specialist

          MAXON Registered Developer

          C4DSC 1 Reply Last reply Reply Quote 1
          • C4DSC
            C4DS @Manuel
            last edited by

            @m_magalhaes said in Getting the State of Modifier Keys (ctrl/cmd, shift, alt):

            Regarding the pre-R20 and r20 code i don't have any idea instead that storing bool in a bool (0 or 1) make more sense.
            In your code pre-R20 you were sending value > 1 to a bool

            I see, I got it all wrong all those years.
            My implementation came from reading the examples users posted on the forum (now the legacy forum).
            Most uses of modifiers keys were checking inside an if-statement, as such I assumed I could move the checking outside of the if and assign it to a Bool instead, to then check on multiple locations in a function.

            Here's an example found in the legacy forum

            On 06/09/2005 at 12:55, xxxxxxxx wrote:
            
            Hi
            
            the qualifiers are combined bitwise. So you can check each one by
            
            if( key&QSHIFT; )
            {
            // shift down
            }
            if( key&CTRL; )
            {
            // ctrl down
            }
            ..
            

            Notice the original message is full of typos, but the idea is what it is. Similar solutions have been posted since, all with this same idea ... that's where I got the bad habit from.
            Thanks for clarifying the use. Will try to remember it for future plugins.

            1 Reply Last reply Reply Quote 0
            • ManuelM
              Manuel
              last edited by

              I'm not sure it's a bad habit or what's the difference. There's maybe type conversion, i don't know.

              Or maybe with the code optimization there's no difference at the end.

              I just wanted to add my 2 cents to have bool and not value xD

              Cheers
              Manuel

              MAXON SDK Specialist

              MAXON Registered Developer

              1 Reply Last reply Reply Quote 0
              • First post
                Last post