Given that the index always changes to 2 regardless of which key is pressed, it suggests that there may be an issue with the order or conditions of your if-elif statements in the script. Specifically, it seems like the condition for key_1_pressed == '1' is always true when any key is pressed, possibly due to overlapping or sticky key states. Let’s further refine and troubleshoot your script:
It’s crucial to ensure that key conditions are exclusive and properly captured. Update your script to add more detailed print statements and ensure conditions are mutually exclusive:
chop_to_dat = op('chopto1')
key_0_pressed = chop_to_dat[0, '0'].val if chop_to_dat[0, '0'] is not None else '0'
key_1_pressed = chop_to_dat[0, '1'].val if chop_to_dat[0, '1'] is not None else '0'
key_2_pressed = chop_to_dat[0, '2'].val if chop_to_dat[0, '2'] is not None else '0'
print("Key 0 State:", key_0_pressed)
print("Key 1 State:", key_1_pressed)
print("Key 2 State:", key_2_pressed)
# Handling resets when no keys are pressed
print("Switching to index 1")
op('switch1').par.index = 1
elif key_1_pressed == '1':
print("Switching to index 2")
op('switch1').par.index = 2
elif key_2_pressed == '1':
print("Switching to index 3")
op('switch1').par.index = 3
print("No keys pressed, resetting index")
op('switch1').par.index = 0 # Reset index or set to default view
Watch the Textport outputs when you press each key. The debug statements should clearly tell you which block of the code is being executed, and help confirm if multiple keys are mistakenly registering as pressed.
If your CHOP to DAT is not updating the way it should when keys are released, the issue might be with how the Keyboard In CHOP is configured or how key state data is managed:
- Non-zero Values: Check if the
chop_to_dat contains non-zero values even when keys are released. You might need to reset the values in your system manually or configure the Keyboard In CHOP to ensure it resets to zero after a key is released.
Sometimes, especially with digital inputs, you might be getting ‘bounces’ or multiple rapid toggles between states when pressing or releasing keys. If your setup allows, try adding a debounce logic or delay to ensure that the state is stable before making changes based on key presses.
Ensure that your comparisons between strings and numbers are consistent ('1' vs 1). While your script uses strings, make sure this matches how data is represented in chop_to_dat.
By following these steps, you should be able to pinpoint why the wrong conditions are being triggered and ensure that each key press correctly adjusts the index of the Switch TOP.