how to make a Combination key in USB_HID?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
xiaowuyi
Posts: 5
Joined: Sat Jun 11, 2016 9:58 am

how to make a Combination key in USB_HID?

Post by xiaowuyi » Sat Jun 11, 2016 10:04 am

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?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how to make a Combination key in USB_HID?

Post by pythoncoder » Sun Jun 12, 2016 9:03 am

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.
Peter Hinch
Index to my micropython libraries.

xiaowuyi
Posts: 5
Joined: Sat Jun 11, 2016 9:58 am

Re: how to make a Combination key in USB_HID?

Post by xiaowuyi » Sun Jun 12, 2016 2:04 pm

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!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how to make a Combination key in USB_HID?

Post by pythoncoder » Mon Jun 13, 2016 6:41 am

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

Code: Select all

Keyboard.set_modifier(MODIFIERKEY_CTRL);
Keyboard.set_key1(KEY_S);
Keyboard.send_now();
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 ;)
Peter Hinch
Index to my micropython libraries.

xiaowuyi
Posts: 5
Joined: Sat Jun 11, 2016 9:58 am

Re: how to make a Combination key in USB_HID?

Post by xiaowuyi » Mon Jun 13, 2016 12:28 pm

I solved this problem。thx!

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: how to make a Combination key in USB_HID?

Post by deshipu » Mon Jun 13, 2016 12:34 pm

Can you post your solution, so that others with similar problem can benefit?

xiaowuyi
Posts: 5
Joined: Sat Jun 11, 2016 9:58 am

Re: how to make a Combination key in USB_HID?

Post by xiaowuyi » Fri Jun 17, 2016 3:04 pm

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.

fpp
Posts: 64
Joined: Wed Jul 20, 2016 12:08 pm

Re: how to make a Combination key in USB_HID?

Post by fpp » Sat Oct 08, 2016 1:08 pm


Post Reply