Skip to content

keyboard_layout.write issues on linux #133

Open
@mcglonelevi

Description

@mcglonelevi

Hello,

I have some code that works as expected on Windows but does not send the keystrokes that I supply on Linux. Here's my code.py:

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

def configure_pin(pin):
    key_pin = digitalio.DigitalInOut(pin)
    key_pin.direction = digitalio.Direction.INPUT
    key_pin.pull = digitalio.Pull.UP
    return key_pin

key_configurations = [
    {
        "pin": configure_pin(board.GP27),
        "macro": "/time set day\n",
        "pressed": False,
    },
    {
        "pin": configure_pin(board.GP26),
        "macro": "/time set night\n",
        "pressed": False,
    },
    {
        "pin": configure_pin(board.GP28),
        "macro": "/tp 500 57 -53\n",
        "pressed": False,
    },
]

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

while True:
    # Check each pin
    for config in key_configurations:
        if not config["pin"].value:  # Is it grounded?
            # print("Pin #%d is grounded." % i)

            if not config["pressed"]:
                keyboard_layout.write(config["macro"], 0.05)  # ...Print the string
            
            config["pressed"] = True
        else:
            # print("Pin #%d is ungrounded." % i)
            config["pressed"] = False

    time.sleep(0.05)

According to dmesg and LSUSB, it looks like the keyboard is recognized as a HID device, but the key strokes are not sending for some reason. This code works fine on Windows. I am using Linux Mint.

Any ideas on troubleshooting?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions