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

    Detect unicode character keypress in a Python script

    General Talk
    2
    6
    1.2k
    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.
    • R
      reubenlara
      last edited by

      Hello! I've successfully been able to detect modifier keys for my Phyton script, but can't figure out how to use "BFM_INPUT_ASC" to detect the letters "x", "y", and "z" (or any other character, for that matter). Where can i query this within the code below? Thank you!

      pressedKey = 'none'
      bc = c4d.BaseContainer()
      if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD,c4d.BFM_INPUT_CHANNEL,bc):
          if bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QSHIFT:
              print("SHIFT PRESSED")
              pressedKey = "shift"
          if bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QCTRL:
              print("CONTROL PRESSED")
              pressedKey = "control"
          if bc[c4d.BFM_INPUT_QUALIFIER] & c4d.QALT:
              print("ALT PRESSED")
              pressedKey = "alt"
      
      R 1 Reply Last reply Reply Quote 0
      • R
        reubenlara @reubenlara
        last edited by

        @reubenlara For that matter, maybe I'm going about it the wrong way with "BFM_INPUT_ASC"? How would I detect normal letters? Thank you!

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

          Hi,

          based on this thread, I write that code

          Of course, be aware that keyboard can change depending on the OS setting.

          import c4d
          #Welcome to the world of Python
          
          
          def main():
              
              bc = c4d.BaseContainer()
              endLoop = False
              while (not endLoop):
                  if c4d.gui.GetInputEvent(c4d.BFM_INPUT_KEYBOARD, bc):
                      key = bc[c4d.BFM_INPUT_CHANNEL]
                      print (key)
                      if key == 69:
                          endLoop = True
                      if key == c4d.NOTOK:
                          return
          
                      print (c4d.gui.Shortcut2String(bc[c4d.BFM_INPUT_QUALIFIER], bc[c4d.BFM_INPUT_CHANNEL]))
          # Execute main()
          if __name__=='__main__':
              main()
          

          you can also check for a specific key pressed:

            # Check for specific key pressed
              bc = c4d.BaseContainer()
              if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD, c4d.KEY_F10,bc):
                  if bc[c4d.BFM_INPUT_VALUE] == 1:
                      print "F10 PRESSED"
                  else:
                      print "F10 NOT PRESSED"
          

          Cheers,
          Manuel

          MAXON SDK Specialist

          MAXON Registered Developer

          R 1 Reply Last reply Reply Quote 0
          • R
            reubenlara @Manuel
            last edited by

            @m_magalhaes Thank you! In your example of "Check for specific key pressed", instead of "c4d.KEY_F10", how can I can I check for a character key? I'd like to use GetInputState vs. GetInputEvent for my specific script.

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

              Hi,

              you can pass the key ascii number or use ord to get that number.

              This code will see if the key e is pressed when you click on execute on the script manager.

              import c4d
              
              # Main function
              def main():
                  # Check for specific key pressed
                  bc = c4d.BaseContainer()
                  # user ord('E') or 69 for the e key. do not use ord('e')
                  if c4d.gui.GetInputState(c4d.BFM_INPUT_KEYBOARD, ord('E'),  bc):
                      if bc[c4d.BFM_INPUT_VALUE] == 1:
                          print ("e PRESSED")
                      else:
                          print ("e NOT PRESSED")
                
              
              # Execute main()
              if __name__=='__main__':
                  main()
              

              Cheers,
              Manuel

              MAXON SDK Specialist

              MAXON Registered Developer

              1 Reply Last reply Reply Quote 1
              • R
                reubenlara
                last edited by

                Ah yes thank you so much!

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