Solved Detect unicode character keypress in a Python script

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"

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

@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!

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

@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.

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

Ah yes thank you so much!