#send is supposed to send the two keys at
1 messages · Page 1 of 1 (latest)
the problem with writing like that is that "send" is called when the list is created, you are not putting a command in the list. What you might want to do is use a keyboard layout, and switch on the type:
- use a string to have it typed with the layout (or multiple strings to type slower)
from adafruit_hid.keyboard_layout_us import KeyboardLayout
layout = KeyboardLayout(keyboard)
emoji_0 = ["L", "A", "s", "e", "!", "!"]
emoji_3 = ["guit", Keycode.DOWN_ARROW, Keycode.ENTER]
- use a sub list or tuple to mark key combos
thing = [(Keycode.ALT, Keycode.F4)]
- test the types to decide what to do
def send_keys(keys):
for code in keys:
if isinstance(code, str):
layout.write(code)
elif isinstance(code, (list, tuple)):
keyboard.send(*code)
elif isinstance(code, int):
keyboard.send(code)
time.sleep(0.01)
...
if neokey[2] and not key_2_state:
print("Button C")
neokey.pixels[2] = 0x00FF00
time.sleep(.05)
keys(emoji_2)
key_2_state = True
it's kind of similar to what the hotkeys project does, if you look at how it parses macros, the for item in sequence: loop
https://learn.adafruit.com/macropad-hotkeys
Thank you so much, as usual, Neradoc! This is really helpful.
I accidentally deleted a long explanation while editing the code. Here's another shot at explaining.
I just need to figure out how the "(code," of this def works. I thought this was you telling me to put my code here but then after I studied the macropad hotkeys I realized that was not the case. What is "code" representing here in the def?
import board
import usb_hid
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
from adafruit_neokey.neokey1x4 import NeoKey1x4
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.keyboard_layout_us import KeyboardLayout
from adafruit_hid.consumer_control_code import ConsumerControlCode
i2c_bus = board.STEMMA_I2C()
neokey = NeoKey1x4(i2c_bus, addr=0x30)
key_0_state = False
key_1_state = False
key_2_state = False
key_3_state = False
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayout(kbd)
cc = ConsumerControl(usb_hid.devices)
key1 = ["L", "A", "s", "e", "!", "!"]
def send_keys(keys):
for code in keys:
if isinstance(code, str):
layout.write(code)
elif isinstance(code, (list, tuple)):
kbd.send(*code)
elif isinstance(code, int):
kbd.send(code)
time.sleep(0.01)
while True:
if neokey[0]:
print("Button A")
ConsumerControlCode.PLAY_PAUSE
neokey.pixels[0] = 0xFF0000
else:
neokey.pixels[0] = 0x0
if neokey[1]:
print("Button B")
ConsumerControlCode.VOLUME_DECREMENT
neokey.pixels[1] = 0xFFFF00
else:
neokey.pixels[1] = 0x0
if neokey[2] and not key_2_state:
print("Button C")
neokey.pixels[2] = 0x00FF00
time.sleep(.05)
keys(key1)
key_2_state = True
else:
neokey.pixels[2] = 0x0
if neokey[3]:
print("Button D")
neokey.pixels[3] = 0x00FFFF
else:
neokey.pixels[3] = 0x0```
just a variable name
the keys are usually represented by keycodes, but can be something else so I just called it code
just an arbitrary choice
Okay, thank you! I guess I need to troubleshoot some more.
I think this is the issue:
I thought def send_keys(keys) was the definition. I must be calling keys(key1) wrong.
yeah it's send_keys() not keys()
Ahh!!!!!
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
cc = ConsumerControl(usb_hid.devices)
# Raise volume.
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
# Pause or resume playback.
cc.send(ConsumerControlCode.PLAY_PAUSE)```
What if I wanted to send something like this on a keypress?
cc = ConsumerControl(usb_hid.devices)
key0 = (cc.send(ConsumerControlCode.VOLUME_INCREMENT))
key1 = [cc.send(ConsumerControlCode.VOLUME_DECREMENT)]
I tried these, but I don't think that's the move. I know that def send_keys hands the "send" portion of key0 and key1 but I can't seem to bridge the gap in my head. Here is the full code:
import time
import board
import usb_hid
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
from adafruit_neokey.neokey1x4 import NeoKey1x4
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.keyboard_layout_us import KeyboardLayout
from adafruit_hid.consumer_control_code import ConsumerControlCode
i2c_bus = board.STEMMA_I2C()
neokey = NeoKey1x4(i2c_bus, addr=0x30)
key0_state = False
key1_state = False
key2_state = False
key3_state = False
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayout(kbd)
cc = ConsumerControl(usb_hid.devices)
key0 = (cc.send(ConsumerControlCode.VOLUME_INCREMENT))
key1 = [cc.send(ConsumerControlCode.VOLUME_DECREMENT)]
key2 = ["l", "a", "s", "e", "r", "s"]
key3 = ["l", "a", "s", "e", "r", "s", "l", "o", "l"]
def send_keys(keys):
for code in keys:
if isinstance(code, str):
layout.write(code)
elif isinstance(code, (list, tuple)):
kbd.send(*code)
elif isinstance(code, int):
kbd.send(code)
time.sleep(0.01)
while True:
if neokey[0] and not key0_state:
print("Button A")
neokey.pixels[0] = 0xFF0000
send_keys(key0)
key0_state = True
time.sleep(.05)
else:
neokey.pixels[0] = 0x0
key0_state = False
if neokey[1] and not key1_state:
print("Button B")
neokey.pixels[1] = 0xFFFF00
send_keys(key1)
key1_state = True
time.sleep(.05)
else:
neokey.pixels[1] = 0x0
key1_state = False
if neokey[2] and not key2_state:
print("Button C")
neokey.pixels[2] = 0x00FF00
send_keys(key2)
key2_state = True
time.sleep(.05)
else:
neokey.pixels[2] = 0x0
key2_state = False
if neokey[3] and not key3_state:
print("Button D")
neokey.pixels[3] = 0x00FFFF
send_keys(key3)
key3_state = True
time.sleep(.05)
else:
neokey.pixels[3] = 0x0
key3_state = False
import usb_hid
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
cc = ConsumerControl(usb_hid.devices)
# Raise volume.
cc.send(ConsumerControlCode.VOLUME_INCREMENT)
# Pause or resume playback.
cc.send(ConsumerControlCode.PLAY_PAUSE)```
What if I wanted to send something like this on a keypress?
cc = ConsumerControl(usb_hid.devices)
key0 = (cc.send(ConsumerControlCode.VOLUME_INCREMENT))
key1 = [cc.send(ConsumerControlCode.VOLUME_DECREMENT)]
I tried these, but I don't think that's the move. I know that def send_keys hands the "send" portion of key0 and key1 but I can't seem to bridge the gap in my head. Here is the full code:
import time
import board
import usb_hid
from adafruit_hid.keycode import Keycode
from adafruit_hid.keyboard import Keyboard
from adafruit_neokey.neokey1x4 import NeoKey1x4
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.keyboard_layout_us import KeyboardLayout
from adafruit_hid.consumer_control_code import ConsumerControlCode
i2c_bus = board.STEMMA_I2C()
neokey = NeoKey1x4(i2c_bus, addr=0x30)
key0_state = False
key1_state = False
key2_state = False
key3_state = False
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayout(kbd)
cc = ConsumerControl(usb_hid.devices)
key0 = (cc.send(ConsumerControlCode.VOLUME_INCREMENT))
key1 = [cc.send(ConsumerControlCode.VOLUME_DECREMENT)]
key2 = ["l", "a", "s", "e", "r", "s"]
key3 = ["l", "a", "s", "e", "r", "s", "l", "o", "l"]
def send_keys(keys):
for code in keys:
if isinstance(code, str):
layout.write(code)
elif isinstance(code, (list, tuple)):
kbd.send(*code)
elif isinstance(code, int):
kbd.send(code)
time.sleep(0.01)
while True:
if neokey[0] and not key0_state:
print("Button A")
neokey.pixels[0] = 0xFF0000
send_keys(key0)
key0_state = True
time.sleep(.05)
else:
neokey.pixels[0] = 0x0
key0_state = False
if neokey[1] and not key1_state:
print("Button B")
neokey.pixels[1] = 0xFFFF00
send_keys(key1)
key1_state = True
time.sleep(.05)
else:
neokey.pixels[1] = 0x0
key1_state = False
if neokey[2] and not key2_state:
print("Button C")
neokey.pixels[2] = 0x00FF00
send_keys(key2)
key2_state = True
time.sleep(.05)
else:
neokey.pixels[2] = 0x0
key2_state = False
if neokey[3] and not key3_state:
print("Button D")
neokey.pixels[3] = 0x00FFFF
send_keys(key3)
key3_state = True
time.sleep(.05)
else:
neokey.pixels[3] = 0x0
key3_state = False
cc.send(ConsumerControlCode.VOLUME_DECREMENT) is a function call (or method call if you use OOP terminology), it sends the control code and returns None, which means you are putting None in your key0 list, instead you want to put something that send_keys will identify as meaning that it must call cc.send with the parameter you give it
also to make a list of a single element x, use [x] or (x,) for a tuple. Because (x) is just "x in parenthesis" it's the same as x
I believe the Hotkeys guide uses dictionaries for advanced types
something like:
key0 = [{"cc": ConsumerControlCode.VOLUME_INCREMENT}]
...
def send_keys(keys):
for code in keys:
if isinstance(code, str):
layout.write(code)
elif isinstance(code, (list, tuple)):
kbd.send(*code)
elif isinstance(code, int):
kbd.send(code)
elif isinstance(code, dict):
if "cc" in code:
cc.send(code["cc"])
time.sleep(0.01)
that way the "code" for the key is a dictionary, the "cc" item in the dictionary is seen as a control code and sent as such
note that Keycodes and ControlCodes are just numbers, they can't be differentiated on their own
This is insanely helpful. I have implemented all of these suggestions and it works great! However, in an effort to really learn this, I am stuck on something that I don't understand. I don't get how it knows which CC code I am pressing. For instance:
key0 = [{"cc": ConsumerControlCode.PLAY_PAUSE}]
key1 = [{"cc": ConsumerControlCode.VOLUME_INCREMENT}]
key2 = [{"cc": ConsumerControlCode.VOLUME_DECREMENT}]```
If I press key0 then it triggers the while loop send_keys() function. It puts key0 into the send_keys function, by which it figures out what data type it is, then sends the appropriate prefix to the while loop to activate the keypress. Is that right?