In one of the applications on the library Keyboard
, try to implement the installation And the subsequent saving of hot (s) keys (s), in order to after restarting the application, the buttons have been loaded, installed and they were not necessary to bind.
Documentation: Link to the documentation on the Keyboard library
Store buttons in the database, I use SQLite
algorithm:
- If in the database is empty, create a
button
value and call the key listening to. - using the bindig function called or keyboard key
- Algorithm defines this key, or several and adds to
keyboard.add_hotkey ()
, after which it saves. - If you are not empty when entering in the database – gets the value and also sets
keyboard.add_hotkey ()
.
Problem:
everything works, but there is one but. The value of the key is defined by the current layout. If you save the key / combination of the keys in Russian, then, when I run the application with an enabled English layout, Keyboard
will give an error about what does not know, for example, the h key “. Similarly, when moving from English layouts to Russian.
- if you save the buttons through the
keyboard.hook ()
, then from the transmitted event, you can get as the name of the button – “k” / “l” and its code – 37. And if the binid is already code, then the problems do not occur, because the button code is common to both layouts. But I need the ability to bing 2 keys, andkeyboard.hook ()
immediately sendsevent
when you click one. - I tried to receive buttons codes via
keyboard.key_to_scan_codes ()
, but in this case they cannot be borne through thekeyboard.add_hotkey ()
method, since it takes the valueKey
either in the form of a string:'alt + f'
or in the form of a number. And if I want to record a key combination in the form ofint
– I will receive the sum of these numbers, that is, a completely different key.
Examples:
keyboard.add_hotkey ('Ctrl + Shift', Callback) ⬅⬅⬅⬅⬅ true
Keyboard.Add_hotkey (29, Callback) ⬅⬅⬅⬅⬅ true (29 - CTRL button code)
keyboard.add_hotkey (16 + 17, callback) ⬅⬅⬅⬅⬅ true (16 - Q, 17 - w, but as a result we get 33 - button F)
keyboard.add_hotkey ('16 +17 ', callback) ⬅⬅⬅⬅⬅ FALSE (16 - Q, 17 - W, but Keyboard says that these are unknown keys)
code: (not sure what is needed)
import keyboard, sqlite3
Def Callback ():
Print ('Function is called')
DEF BIND_KEYS ():
Print ('Press the / COMMUNICATION key' key ')
Key = Keyboard.Read_HotKey (Suppress = False)
Key = Str (Key) .split ('+')
IF Len (Key) == 2:
Print (F "Two Two Buttons: {Key [0] .upper ()}, {Key [1] .upper ()} ')
Key = F '{Key [0]} + {Key [1]}'
ELIF LEN (KEY) == 1:
Print (F "One button: {Key [0] .upper ()} ')
Key = Key [0]
Keyboard.add_hotkey (Key, Callback, Suppress = False)
SQL.EXECUTE (F "Select Button from Test WHERE Button = '{Key}'")
if sql.fetchone () is none:
SQL.EXECUTE (F "INSERT INTO TEST VALUES ('{KEY}')")
db.commit ()
Print ('keys saved')
keyboard.wait ()
DB = SQLite3.Connect ('db.db')
SQL = db.cursor ()
SQL.EXECUTE ("CREATE TABLE IF NOT EXISTS TEST (Button Text)")
db.commit ()
SQL.EXECUTE ("SELECT * FROM TEST")
Key = SQL.Fetchone ()
If Key! = None:
Keyboard.add_hotkey (Key [0], Callback, Suppress = False)
keyboard.wait ()
ELSE:
bind_keys ()
Answer 1, Authority 100%
The most logical – save key names in dictionary of type
keys = {
'W': 'C',
'A': 'F',
's': 's',
'D': 'in'
}
And then, checking the current layout, like this
import ctypes
def get_layout ():
u = ctypes.windll.LoadLibrary ("user32.dll")
pf = getattr (u, "GetKeyboardLayout")
if hex (pf (0)) == '0x4190419':
return 'ru'
if hex (pf (0)) == '0x4090409':
return 'en'
Set keyboard shortcuts already by dictionary values:
keyboard.add_hotkey (f "{keys ['ctrl']} + {keys ['shift']}" , callback)