boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.76k stars 433 forks source link

Official documentation no mention of all possible keys especially numpad keys #566

Open hkunz opened 1 year ago

hkunz commented 1 year ago

I tried to simulate the numpad 7 function but I don't know what name is used for it and I can't find anything in the official documentation relating to numpad presses https://pypi.org/project/keyboard/. Basically what I was trying to do is:

import keyboard
keyboard.press_and_release('numpad7')
Erriez commented 1 year ago

@hkunz Which OS are you using? On Windows 10 it is keyboard.press_and_release('num 7').

However, I'm not able to send the * or / key on the numpad as this sends the normal keys.

I could not find numpad documentation.

high-solutions commented 1 year ago

Google the keycode numbers and us those?

Erriez commented 1 year ago

I could not find a Windows application in source which can send a separate key code for the * (shift+8) and*` on the keypad. Any suggestion?

BackMountainDevil commented 1 year ago

keyboard.press_and_release('7')

Erriez commented 1 year ago

keyboard.press_and_release('7')

On a US QWERTY keyboard, this command sends number 7 (shared with &) instead of key 7 (shared with Home) on the numpad.

Still not clear how to send numpad 7.

BackMountainDevil commented 1 year ago

numpad7 is not unmber 7?I use US qwert,too

From An idea

在 2023年3月25日,23:12,Erriez @.***> 写道:

keyboard.press_and_release('7')

On a US QWERTY keyboard, this command sends number 7 (shared with &) instead of key 7 (shared with Home) on the numpad.

Still not clear how to send numpad 7.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

Erriez commented 1 year ago

Unfortunately the normal number 7 and numpad 7 are two different keys with different functionality. I could not find an example how to send numpad 7 which can be recognized by my music application REAPER as shown in the screenshot below:

image

BackMountainDevil commented 1 year ago

My apology. I see '[' is in the same line of Num7. Can that work?

From An idea

在 2023年3月25日,23:33,Erriez @.***> 写道:

Unfortunately the normal number 7 and numpad 7 are two different keys with different functionality. I could not find an example how to send numpad 7 which can be recognized by my music application REAPER as shown in the screenshot below:

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

Erriez commented 1 year ago

Yes, I use that as workaround as I cannot send numpad keys.

MicroElf commented 1 year ago

I have the same problem with numpad keys. When I use keyboard.add_hotkey('num 2', callback) it will trigger with both 2 (=@) and numpad 2. Is there any way to distinguish between these keys?

I also tried numeric value of 98 for numpad 2 key but it didn't work.

MicroElf commented 1 year ago

I found 2 possible solutions:

  1. Use numeric scan codes. For my keyboard numpad 2 is for some reason 80 not 98.
  2. Change keyboard._canonical_names.canonical_names numpad section from 'num 2': '2' to 'num 2': 'num 2'.
Erriez commented 1 year ago

Use numeric scan codes. For my keyboard numpad 2 is for some reason 80 not 98.

Correct, I can reproduce it as 'num 7': '7', sends key vk = 55 (normal 7 key) instead of vk = 103 (numeric keypad 7) in function _send_event code user32.keybd_event(vk, code, event_type, 0).

Change keyboard._canonical_names.canonical_names numpad section from 'num 2': '2' to 'num 2': 'num 2'.

This is not sufficient for Windows to send numpad keys.

Function _setup_name_tables() reads keycodes via the Windows user32 API:

# all_scan_codes does not contain numpad keys 0..9 (codes 96..105):
all_scan_codes = [(sc, user32.MapVirtualKeyExW(sc, MAPVK_VSC_TO_VK_EX, 0)) for sc in range(0x100)]

# all_vks contains numpad key 0..9 (codes 96..105):
all_vks =        [(user32.MapVirtualKeyExW(vk, MAPVK_VK_TO_VSC_EX, 0), vk) for vk in range(0x100)]

# For loop:
        for scan_code, vk in all_scan_codes + all_vks:
           ...
            # The numpad information is lost in dict scan_code_to_vk as it only contains the scan_code and vk:
            if scan_code not in scan_code_to_vk:
                scan_code_to_vk[scan_code] = vk