pimoroni / pmk-circuitpython

MIT License
86 stars 21 forks source link

How does one set up holding keys only while the key is pressed down? #5

Closed AstroOrbis closed 2 years ago

AstroOrbis commented 2 years ago

I want to map the bottom 4 keys to DFJK (Kudos if you know why)

Problem is, when I hold it, it just holds for the default amount of time then automatically releases.

How would I make it hold for the duration that I'm holding it, no longer and no shorter?

(The answers are probably in the docs somewhere and I'm just stupid lol)

Gadgetoid commented 2 years ago

That's weird- holding down the key should "press" it, but not release until it's physically released. Can you post the code you're using?

I- too- had a Taiko no Tatsujin addiction :laughing:

image

AstroOrbis commented 2 years ago

I don't have access to my code right now, but I'll send it to you in 15 minutes!

Also, I didn't expect to find a Taiko player here! I'm more of a 4k person, seeing as I'm not the best at Taiko :3

AstroOrbis commented 2 years ago
# SPDX-FileCopyrightText: 2021 Sandy Macdonald
#
# SPDX-License-Identifier: MIT

# A simple example of how to set up a keymap and HID keyboard on Keybow 2040.

# You'll need to connect Keybow 2040 to a computer, as you would with a regular
# USB keyboard.

# Drop the keybow2040.py file into your `lib` folder on your `CIRCUITPY` drive.

# NOTE! Requires the adafruit_hid CircuitPython library also!

import board
from keybow2040 import Keybow2040

import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

# Set up Keybow
i2c = board.I2C()
keybow = Keybow2040(i2c)
keys = keybow.keys

# Set up the keyboard and layout
keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)

# A map of keycodes that will be mapped sequentially to each of the keys, 0-15

# FX 

FX_L = Keycode.C
FX_L2 = Keycode.C
FX_R = Keycode.M
FX_R2 = Keycode.M
#BT
BT_A = Keycode.D
BT_B = Keycode.F
BT_C = Keycode.J
BT_D = Keycode.K
#VOL
VOL_LL = Keycode.W
VOL_LR = Keycode.E
VOL_RL = Keycode.O
VOL_RR = Keycode.P

keymap =  [FX_L, BT_A, VOL_LL, 0, FX_L2, BT_B, VOL_LR, 0, FX_R, BT_C, VOL_RL, 0, FX_R2, BT_D, VOL_RR, 0]

# The colour to set the keys when pressed, yellow.
rgb = (255, 255, 255)

# Attach handler functions to all of the keys
for key in keys:
    # A press handler that sends the keycode and turns on the LED
    @keybow.on_press(key)
    def press_handler(key):
        keycode = keymap[key.number]
        keyboard.send(keycode)
        key.set_led(*rgb)

    # A release handler that turns off the LED
    @keybow.on_release(key)
    def release_handler(key):
        key.led_off()

while True:
    # Always remember to call keybow.update()!
    keybow.update()

Here is the full code I'm using, it's optimized so I can use it for Taiko, USC, and Quaver. I'm using Quaver to test it, and when I try using DFJK, the corresponding lane judgement bar flashes for a small second. It's probably registering the keypress then letting go for a few frames, and holding doesn't do anything either.

Gadgetoid commented 2 years ago

Ah, I see the problem: "keyboard.send(keycode)" will - indeed- just send a brief key press.

You need to replace your handlers with something like this:

for key in keys:
    # A press handler that sends the keycode and turns on the LED
    @keybow.on_press(key)
    def press_handler(key):
        keycode = keymap[key.number]
        keyboard.press(keycode)
        key.set_led(*rgb)

    # A release handler that turns off the LED
    @keybow.on_release(key)
    def release_handler(key):
        keycode = keymap[key.number]
        keyboard.release(keycode)
        key.led_off()

Will be interested to know how well this works. Certainly worth including as an example... and uh better than brutalizing my laptops integrated and non replaceable keys. (though I had Taiko on Xbox Games Pass which will expire soon waah)

AstroOrbis commented 2 years ago

Do you have a Discord by any chance? I'll try this when I have access to my code again.

Gadgetoid commented 2 years ago

I can usually be found on the Pimoroni Discord (with some effort :laughing:) and others (with less :laughing:) - https://discord.gg/gKrpFyHB3K

AstroOrbis commented 2 years ago

It works perfectly, thanks!