Skip to content

Conversation

@iyalosovetsky
Copy link

I am very impressed with how the USB host keyboard works. For my CNC project, I `ve to added the ability to work with the function keys f1-f12, ctrl-left, ctrl-right, ctrl-up, ctrl-down, page down, page up, insert, delete, pause in the format of command VT 100. Offer my modified code "supervisor/shared/usb/host_keyboard.c"

Added the ability to work with the function keys f1-f12, ctrl-left, ctrl-right, ctrl-up, ctrl-down, page down, page up, insert, delete, pause. in the format of command VT 100
@tannewt
Copy link
Member

tannewt commented Jan 17, 2024

Please run pre-commit over your code: https://learn.adafruit.com/improve-your-code-with-pylint/check-your-code-with-pre-commit It should automatically fix these issue before commit.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements! Just a couple suggestions.

{ HID_KEY_ENTER, HID_KEY_SLASH, 0, 0, "\r\x1b\10\t -=[]\\#;'`,./" },

{ HID_KEY_F1, HID_KEY_F1, 0x1e, 0, }, // help key on xerox 820 kbd
// { HID_KEY_F1, HID_KEY_F1, 0x1e, 0, }, // help key on xerox 820 kbd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// { HID_KEY_F1, HID_KEY_F1, 0x1e, 0, }, // help key on xerox 820 kbd

@iyalosovetsky
Copy link
Author

iyalosovetsky commented Jan 18, 2024 via email

@jepler
Copy link

jepler commented Jan 18, 2024

. if it`s important to leave compatibility with xerox 820 keyboard I can change to the previous version.

No, this is not a priority for me.

Comment on lines 296 to 298
if (!(mapper->flags & FLAG_CTRL) && ctrl) {
continue;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this change doesn't have the desired effect.

When ctrl+c is pressed, the result needs to be the interrupt character with value 3.

This happens through the keycode mapper element { HID_KEY_A, HID_KEY_Z, 'a', 0, NULL}, which does not have FLAG_CTRL set, so the initial code is the ASCII value 'c'. Then this is modified (at line 258 before this change) with a bitmask:

                if (ctrl) {
                    code &= 0x1f;
                }

I believe, but didn't test, that with your change the rule for KEY_A..KEY_Z will "continue" at new line 297 rather than sending the interrupt character.

I am guessing that you made this change so that your new code will send the desired code for ctrl+ARROW_UP and so on, but merely putting the entries in the correct order (with the FLAG_CTRL entry first) should suffice.

Copy link
Author

@iyalosovetsky iyalosovetsky Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right!
I've changed to

                if (!(mapper->flags & FLAG_CTRL) && ctrl && (mapper->flags & FLAG_STRING)) {
                    continue;
                }

and it works fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best way would be to set a range of special keys, but no idea if that's possible or not.

{ HID_KEY_F12, HID_KEY_F12, 0, FLAG_STRING, F12 },
{ HID_KEY_PRINT_SCREEN, HID_KEY_PRINT_SCREEN, 0, FLAG_STRING, PRINT_SCREEN },

{ HID_KEY_ARROW_UP, HID_KEY_ARROW_UP, 0 , FLAG_STRING+FLAG_CTRL,CTRL_UP },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more accustomed to the style where bit masks are combined with "|" instead of "+" and would prefer it that way here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

{ HID_KEY_INSERT, HID_KEY_INSERT, 0, FLAG_STRING, CURSOR_INS },
{ HID_KEY_DELETE, HID_KEY_DELETE, 0, FLAG_STRING, CURSOR_DEL },

{ HID_KEY_F1, HID_KEY_F1, 0, FLAG_STRING, F1 },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When USB keycodes are consecutive, which F1..F12 and RIGHT...UP are, it saves a small amount (maybe 8 bytes per entry) of flash storage to use the form which accommodates multiple strings within one entry, { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_UP, 0, FLAG_STRING, CURSOR_RIGHT SEP CURSOR_LEFT SEP CURSOR_DOWN SEP CURSOR_UP },

This is done somewhat inconsistently in the original (for instance, INSERT..ARROW_UP are consecutive but the table does not use this fact) but it would be nice if it were done here for these 12 consecutive items, it might save about 100 bytes of flash storage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

#define F10 "\e[21~"
#define F11 "\e[23~"
#define F12 "\e[24~"
#define PRINT_SCREEN "\e[i"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Just a note for myself: yes there really is a code defined for the printscreen key! xterm and rxvt-unicode both use this value]

Comment on lines +83 to +87
#define F1 "\eOP"
#define F2 "\eOQ"
#define F3 "\eOR"
#define F4 "\eOS"
#define F5 "\e[15~"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly, my documentation ("man 7 urxvt") has "\e[11~" through "\e14~" for F1..F4. "\eOP" .. "\eOS" are for KP_F1..KP_F4, which do not exist on traditional PC keyboards. however both xterm and xfce4-terminal are behaving like you've implemented here, so I'm sure it's fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. I've change to "\e[11~" through "\e14~"

@iyalosovetsky
Copy link
Author

iyalosovetsky commented Jan 18, 2024 via email

@iyalosovetsky
Copy link
Author

iyalosovetsky commented Jan 19, 2024

I've tested on my pico by code bellow. Look like good.

import microcontroller
#microcontroller.cpu.frequency = 120000000
print(microcontroller.cpu.frequency)
import time
import sys
import board

import usb_host

import supervisor
pp=usb_host.Port(board.GP26, board.GP27)
proceedCh=''    
while True:
  try:  
    proceedCh=''    
    while supervisor.runtime.serial_bytes_available:
        proceedCh += sys.stdin.read(1)
    if proceedCh!='':
      print('proceedCh:',proceedCh,[ord(res) for res in proceedCh])
    time.sleep(0.1)
  except KeyboardInterrupt:
        print('main: will cancel proceedCh:',proceedCh,[ord(res) for res in proceedCh])
        break

print("end.")
time.sleep(5)

@tannewt tannewt requested a review from jepler January 23, 2024 00:07
@tannewt tannewt dismissed their stale review January 23, 2024 00:08

leave up to jepler

this means that the test for FLAG_CTRL together with FLAG_STRING is
not needed. Instead, the FLAG_STRING | FLAG_CTRL row, when
encountered earlier, will automatically take precedence.
This bug was always present in the key map.
Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I fixed a few small items, see the commit messages. Now good to merge, assuming the CI comes up green.

@iyalosovetsky
Copy link
Author

iyalosovetsky commented Jan 28, 2024

Thanks! I have compliled , flashed and double checked on my Raspberry pico. All work fine!!

@jepler jepler merged commit ebe8390 into adafruit:main Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants