0

Button presses not always registered in games
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Button presses not always registered in games

by Martin101 on Thu Jun 27, 2019 3:06 pm

Hello, I just wired up two tactile switches to a Trinket M0. I want to add a couple extra buttons to my Steam Controller for gaming.
The presses register just fine in text docs or in web browsers, but when I fire up a game in Steam, no matter which game I try the button presses are intermittent. Sometimes they'll register a couple times in a row, sometimes it takes 3 or more presses to get it to register. Even from a dead rest when pressing one of the two buttons for the first time, most of the time it doesn't respond. Once in awhile it responds on the first attempt, but then subsequent presses have problems.

Any ideas?

*Also, can anyone tell me how to add a "hold pressed buttons" to the code?

Code: Select all | TOGGLE FULL SIZE
# CircuitPython demo - Keyboard emulator

import time

import board
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

# A simple neat keyboard demo in CircuitPython

# The pins we'll use, each will have an internal pullup
keypress_pins = [board.A1, board.A2]
# Our array of key objects
key_pin_array = []
# The Keycode sent for each button, will be paired with a control key
keys_pressed = [Keycode.C, Keycode.SPACEBAR]

# The keyboard object!
time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard()
keyboard_layout = KeyboardLayoutUS(keyboard)  # We're in the US :)

# Make all pin objects inputs with pullups
for pin in keypress_pins:
    key_pin = digitalio.DigitalInOut(pin)
    key_pin.direction = digitalio.Direction.INPUT
    key_pin.pull = digitalio.Pull.UP
    key_pin_array.append(key_pin)

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

print("Waiting for key pin...")

while True:
    # Check each pin
    for key_pin in key_pin_array:
        if not key_pin.value:  # Is it grounded?
            i = key_pin_array.index(key_pin)
            print("Pin #%d is grounded." % i)

            # Turn on the red LED
            led.value = True

            while not key_pin.value:
                pass  # Wait for it to be ungrounded!
            # "Type" the Keycode or string
            key = keys_pressed[i]  # Get the corresponding Keycode or string
            if isinstance(key, str):  # If it's a string...
                keyboard_layout.write(key)  # ...Print the string
            else:  # If it's not a string...
                keyboard.press(key)  # "Press"...
                keyboard.release_all()  # ..."Release"!

            # Turn off the red LED
            led.value = False

    time.sleep(0.01)


I've used this code as well.
Code: Select all | TOGGLE FULL SIZE
import digitalio
from board import *
import time
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# A simple neat keyboard demo in circuitpython
buttonpins = [D0, D2]


# The keycode sent for each button, optionally can be paired with a control key
buttonkeys = [6, 44]



# the keyboard object!
kbd = Keyboard()

# our array of button objects
buttons = []

# make all pin objects, make them inputs w/pullups
for pin in buttonpins:
    button = digitalio.DigitalInOut(pin)
    button.direction = digitalio.Direction.INPUT
    button.pull = digitalio.Pull.UP
    buttons.append(button)

print("Waiting for button presses")

while True:
    # check each button
    for button in buttons:
        if (not button.value):   # pressed?
            i = buttons.index(button)

            print("Button #%d Pressed" % i)

            # type the keycode!
            k = buttonkeys[i]    # get the corresp. keycode
            kbd.press(k)
            # Use this line for key combos kbd.press(k, controlkey)
            kbd.release_all()

            while (not button.value):
                pass  # wait for it to be released!
    time.sleep(0.01)

Martin101
 
Posts: 1
Joined: Thu Jun 27, 2019 2:59 pm

Re: Button presses not always registered in games

by XRAD on Tue Jul 02, 2019 8:04 pm

bouncetime? button debounce may help with the first issue....

XRAD
 
Posts: 440
Joined: Sat Nov 19, 2016 3:28 pm

Please be positive and constructive with your questions and comments.