I want to make a Combination key such as shift+a,but i dont know how to do.
This is what i tried:
#boot.py
import machine
import pyb
#pyb.main('main.py') # main script to run after this one
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
pyb.usb_mode('CDC+HID',hid=pyb.hid_keyboard)
#main.py
hid=pyb.USB_HID()
buf=bytearray(8)
buf[2]=0xE1 #left shift
hid.send(buf)#press left shift
pyb.delay(10)
buf[2]=0x04#a
hid.send(buf)#press a
pyb.delay(10)
buf[2]=0
hid.send(buf)
but it cant work.how can i make a Combination key?
how to make a Combination key in USB_HID?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: how to make a Combination key in USB_HID?
I've looked at some code I wrote a few years ago for making a keyboard interface with an Arduino and it looks comparable to yours. One question is whether you need to send two release key commands, one for the 'a' key and the other for the shift key. Although this probably doesn't matter unless you're sending subsequent characters.
Another thought is that main.py is executed almost immediately after the Pyboard is plugged in to the host machine. The latter may not have recognised the Pyboard so soon. I'd try a delay of several seconds before the first instruction.
Another thought is that main.py is executed almost immediately after the Pyboard is plugged in to the host machine. The latter may not have recognised the Pyboard so soon. I'd try a delay of several seconds before the first instruction.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: how to make a Combination key in USB_HID?
thx for your reply. However i believe the problem is still not finished. you mentioned about the delay of a couple seconds before the first instruction, i've already done this. There's nothing wrong with this. I used the following code to finish the operation of ctrl+s on adruino or teensy before.
Keyboard.set_modifier(MODIFIERKEY_CTRL);
Keyboard.set_key1(KEY_S);
Keyboard.send_now();
the problem is that i can't do this in micropython may because that i haven't understand the principle of USB-HID completely. Could you pls tell how to program the code if i want pyboard to complete the operation of ctrl+S.
In my codes:
hid=pyb.USB_HID()
buf=bytearray(8)
buf[2]=0xE1 #left shift
hid.send(buf)#press left shift
pyb.delay(10)
I found out that it's fine to let 'buf[2]=0x04 print out a', while it's not working for 0xE1->shift correpondingly. Because for some keys like a,b,c etc, it will response once the keys are pressed. while for others such as shift, gui, ctrl, PC only responses while the key is released.it seems that mycodes are not always working, could you explain that to me a little bit?
thank u!
Keyboard.set_modifier(MODIFIERKEY_CTRL);
Keyboard.set_key1(KEY_S);
Keyboard.send_now();
the problem is that i can't do this in micropython may because that i haven't understand the principle of USB-HID completely. Could you pls tell how to program the code if i want pyboard to complete the operation of ctrl+S.
In my codes:
hid=pyb.USB_HID()
buf=bytearray(8)
buf[2]=0xE1 #left shift
hid.send(buf)#press left shift
pyb.delay(10)
I found out that it's fine to let 'buf[2]=0x04 print out a', while it's not working for 0xE1->shift correpondingly. Because for some keys like a,b,c etc, it will response once the keys are pressed. while for others such as shift, gui, ctrl, PC only responses while the key is released.it seems that mycodes are not always working, could you explain that to me a little bit?
thank u!
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: how to make a Combination key in USB_HID?
Sorry, I'm out of my depth here. Evidently your code is working. The application I wrote didn't need to send control characters so I don't know how to do this. This example
seems to suggest that you send only one buffer whose contents are modified by the control key and the letter key before being sent. I think you're going to have to research this on the web. Sorry I can't offer any further help - good luck
Code: Select all
Keyboard.set_modifier(MODIFIERKEY_CTRL);
Keyboard.set_key1(KEY_S);
Keyboard.send_now();
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: how to make a Combination key in USB_HID?
I solved this problem。thx!
Re: how to make a Combination key in USB_HID?
Can you post your solution, so that others with similar problem can benefit?
Re: how to make a Combination key in USB_HID?
Keyboard send the date to PC
BYTE1 BYTE2 BYTE3 BYTE4 BYTE5 BYTE6 BYTE7 BYTE8
here:
BYTE1 --
|--bit0: Left Control if push down is 1
|--bit1: Left Shift if push down is 1
|--bit2: Left Alt if push down is 1
|--bit3: Left GUI if push down is 1
|--bit4: Right Control if push down is 1
|--bit5: Right Shift if push down is 1
|--bit6: Right Alt if push down is 1
|--bit7: Right GUI if push down is 1
BYTE3 to BYTE8 is the key.
eg.
left shift + a : 0x02,0x00,0x04,0x00,0x00,0x00,0x00,0x00.
BYTE1 BYTE2 BYTE3 BYTE4 BYTE5 BYTE6 BYTE7 BYTE8
here:
BYTE1 --
|--bit0: Left Control if push down is 1
|--bit1: Left Shift if push down is 1
|--bit2: Left Alt if push down is 1
|--bit3: Left GUI if push down is 1
|--bit4: Right Control if push down is 1
|--bit5: Right Shift if push down is 1
|--bit6: Right Alt if push down is 1
|--bit7: Right GUI if push down is 1
BYTE3 to BYTE8 is the key.
eg.
left shift + a : 0x02,0x00,0x04,0x00,0x00,0x00,0x00,0x00.