Bonjoir !

Après 5 ans confronté à ce "problème" je me suis dit qu'il était temps de comprendre pourquoi je dois procéder ainsi avec le module Pynput (doc). Quelqu'un pourrait-il m'expliquer pourquoi je dois passer par str(key) pour que ça marche ? Comment il fait le print pour afficher 'a' si c'est une instance de classe ? Je sens qu'il me manque des connaissances basiques...

(PS : En réalité j'utilise dans mes conditions key.vk ou key.char mais je suis obligé de passer par des try except car key n'a pas toujours l'attribut voulu)

Un bout de mon script :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
def on_release(key):
    print(key)
    print(type(key))
    print(dir(key))
    if key == "'a'":
        print("toto")
    if str(key) == "'a'":
        print("titi")
 
with keyboard.Listener(
        on_release=on_release) as listener:
    listener.join()
Résultat quand je presse la touche A de mon clavier :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
'a'
<class 'pynput.keyboard._win32.KeyCode'>
['_PLATFORM_EXTENSIONS', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_flags', '_from_ext', '_parameters', '_scan', 'char', 'combining', 'from_char', 'from_dead', 'from_vk', 'is_dead', 'join', 'vk']
titi
Pour ceux qui auraient besoin de voir le module _win32 de Pynput :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# coding=utf-8
# pynput
# Copyright (C) 2015-2019 Moses Palmér
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
The keyboard implementation for *Windows*.
"""
 
# pylint: disable=C0111
# The documentation is extracted from the base classes
 
# pylint: disable=R0903
# We implement stubs
 
import contextlib
import ctypes
import enum
import six
 
from ctypes import wintypes
 
import pynput._util.win32_vks as VK
 
from pynput._util import AbstractListener
from pynput._util.win32 import (
    INPUT,
    INPUT_union,
    KEYBDINPUT,
    KeyTranslator,
    ListenerMixin,
    SendInput,
    SystemHook,
    VkKeyScan)
from . import _base
 
 
class KeyCode(_base.KeyCode):
    _PLATFORM_EXTENSIONS = (
        # Any extra flags.
        '_flags',
 
        #: The scan code.
        '_scan',
    )
 
    # Be explicit about fields
    _flags = None
    _scan = None
 
    def _parameters(self, is_press):
        """The parameters to pass to ``SendInput`` to generate this key.
 
        :param bool is_press: Whether to generate a press event.
 
        :return: all arguments to pass to ``SendInput`` for this key
 
        :rtype: dict
        """
        if self.vk:
            vk = self.vk
            scan = self._scan or 0
            flags = 0
        else:
            res = VkKeyScan(self.char)
            if (res >> 8) & 0xFF == 0:
                vk = res & 0xFF
                scan = self._scan or 0
                flags = 0
            else:
                vk = 0
                scan = ord(self.char)
                flags = KEYBDINPUT.UNICODE
        state_flags = (KEYBDINPUT.KEYUP if not is_press else 0)
        return dict(
            dwFlags=(self._flags or 0) | flags | state_flags,
            wVk=vk,
            wScan=scan)
 
    @classmethod
    def _from_ext(cls, vk, **kwargs):
        """Creates an extended key code.
 
        :param vk: The virtual key code.
 
        :param kwargs: Any other parameters to pass.
 
        :return: a key code
        """
        return cls.from_vk(vk, _flags=KEYBDINPUT.EXTENDEDKEY, **kwargs)
 
 
# pylint: disable=W0212
class Key(enum.Enum):
    alt = KeyCode.from_vk(VK.MENU)
    alt_l = KeyCode.from_vk(VK.LMENU)
    alt_r = KeyCode._from_ext(VK.RMENU)
    alt_gr = KeyCode.from_vk(VK.RMENU)
    backspace = KeyCode.from_vk(VK.BACK)
    caps_lock = KeyCode.from_vk(VK.CAPITAL)
    cmd = KeyCode.from_vk(VK.LWIN)
    cmd_l = KeyCode.from_vk(VK.LWIN)
    cmd_r = KeyCode.from_vk(VK.RWIN)
    ctrl = KeyCode.from_vk(VK.CONTROL)
    ctrl_l = KeyCode.from_vk(VK.LCONTROL)
    ctrl_r = KeyCode._from_ext(VK.RCONTROL)
    delete = KeyCode._from_ext(VK.DELETE)
    down = KeyCode._from_ext(VK.DOWN)
    end = KeyCode._from_ext(VK.END)
    enter = KeyCode.from_vk(VK.RETURN)
    esc = KeyCode.from_vk(VK.ESCAPE)
    f1 = KeyCode.from_vk(VK.F1)
    f2 = KeyCode.from_vk(VK.F2)
    f3 = KeyCode.from_vk(VK.F3)
    f4 = KeyCode.from_vk(VK.F4)
    f5 = KeyCode.from_vk(VK.F5)
    f6 = KeyCode.from_vk(VK.F6)
    f7 = KeyCode.from_vk(VK.F7)
    f8 = KeyCode.from_vk(VK.F8)
    f9 = KeyCode.from_vk(VK.F9)
    f10 = KeyCode.from_vk(VK.F10)
    f11 = KeyCode.from_vk(VK.F11)
    f12 = KeyCode.from_vk(VK.F12)
    f13 = KeyCode.from_vk(VK.F13)
    f14 = KeyCode.from_vk(VK.F14)
    f15 = KeyCode.from_vk(VK.F15)
    f16 = KeyCode.from_vk(VK.F16)
    f17 = KeyCode.from_vk(VK.F17)
    f18 = KeyCode.from_vk(VK.F18)
    f19 = KeyCode.from_vk(VK.F19)
    f20 = KeyCode.from_vk(VK.F20)
    home = KeyCode._from_ext(VK.HOME)
    left = KeyCode._from_ext(VK.LEFT)
    page_down = KeyCode._from_ext(VK.NEXT)
    page_up = KeyCode._from_ext(VK.PRIOR)
    right = KeyCode._from_ext(VK.RIGHT)
    shift = KeyCode.from_vk(VK.LSHIFT)
    shift_l = KeyCode.from_vk(VK.LSHIFT)
    shift_r = KeyCode.from_vk(VK.RSHIFT)
    space = KeyCode.from_vk(VK.SPACE, char=' ')
    tab = KeyCode.from_vk(VK.TAB)
    up = KeyCode._from_ext(VK.UP)
 
    media_play_pause = KeyCode._from_ext(VK.MEDIA_PLAY_PAUSE)
    media_volume_mute = KeyCode._from_ext(VK.VOLUME_MUTE)
    media_volume_down = KeyCode._from_ext(VK.VOLUME_DOWN)
    media_volume_up = KeyCode._from_ext(VK.VOLUME_UP)
    media_previous = KeyCode._from_ext(VK.MEDIA_PREV_TRACK)
    media_next = KeyCode._from_ext(VK.MEDIA_NEXT_TRACK)
 
    insert = KeyCode._from_ext(VK.INSERT)
    menu = KeyCode.from_vk(VK.APPS)
    num_lock = KeyCode._from_ext(VK.NUMLOCK)
    pause = KeyCode.from_vk(VK.PAUSE)
    print_screen = KeyCode._from_ext(VK.SNAPSHOT)
    scroll_lock = KeyCode.from_vk(VK.SCROLL)
# pylint: enable=W0212
 
 
class Controller(_base.Controller):
    _KeyCode = KeyCode
    _Key = Key
 
    def __init__(self, *args, **kwargs):
        super(Controller, self).__init__(*args, **kwargs)
 
    def _handle(self, key, is_press):
        SendInput(
            1,
            ctypes.byref(INPUT(
                type=INPUT.KEYBOARD,
                value=INPUT_union(
                    ki=KEYBDINPUT(**key._parameters(is_press))))),
            ctypes.sizeof(INPUT))
 
 
class Listener(ListenerMixin, _base.Listener):
    #: The Windows hook ID for low level keyboard events, ``WH_KEYBOARD_LL``
    _EVENTS = 13
 
    _WM_INPUTLANGCHANGE = 0x0051
    _WM_KEYDOWN = 0x0100
    _WM_KEYUP = 0x0101
    _WM_SYSKEYDOWN = 0x0104
    _WM_SYSKEYUP = 0x0105
 
    # A bit flag attached to messages indicating that the payload is an actual
    # UTF-16 character code
    _UTF16_FLAG = 0x1000
 
    # A special virtual key code designating unicode characters
    _VK_PACKET = 0xE7
 
    #: The messages that correspond to a key press
    _PRESS_MESSAGES = (_WM_KEYDOWN, _WM_SYSKEYDOWN)
 
    #: The messages that correspond to a key release
    _RELEASE_MESSAGES = (_WM_KEYUP, _WM_SYSKEYUP)
 
    #: Additional window messages to propagate to the subclass handler.
    _WM_NOTIFICATIONS = (
        _WM_INPUTLANGCHANGE,
    )
 
    #: A mapping from keysym to special key
    _SPECIAL_KEYS = {
        key.value.vk: key
        for key in Key}
 
    _HANDLED_EXCEPTIONS = (
        SystemHook.SuppressException,)
 
    class _KBDLLHOOKSTRUCT(ctypes.Structure):
        """Contains information about a mouse event passed to a
        ``WH_KEYBOARD_LL`` hook procedure, ``LowLevelKeyboardProc``.
        """
        _fields_ = [
            ('vkCode', wintypes.DWORD),
            ('scanCode', wintypes.DWORD),
            ('flags', wintypes.DWORD),
            ('time', wintypes.DWORD),
            ('dwExtraInfo', ctypes.c_void_p)]
 
    #: A pointer to a :class:`KBDLLHOOKSTRUCT`
    _LPKBDLLHOOKSTRUCT = ctypes.POINTER(_KBDLLHOOKSTRUCT)
 
    def __init__(self, *args, **kwargs):
        super(Listener, self).__init__(*args, **kwargs)
        self._translator = KeyTranslator()
        self._event_filter = self._options.get(
            'event_filter',
            lambda msg, data: True)
 
    def _convert(self, code, msg, lpdata):
        if code != SystemHook.HC_ACTION:
            return
 
        data = ctypes.cast(lpdata, self._LPKBDLLHOOKSTRUCT).contents
        is_packet = data.vkCode == self._VK_PACKET
 
        # Suppress further propagation of the event if it is filtered
        if self._event_filter(msg, data) is False:
            return None
        elif is_packet:
            return (msg | self._UTF16_FLAG, data.scanCode)
        else:
            return (msg, data.vkCode)
 
    @AbstractListener._emitter
    def _process(self, wparam, lparam):
        msg = wparam
        vk = lparam
 
        # If the key has the UTF-16 flag, we treat it as a unicode character,
        # otherwise convert the event to a KeyCode; this may fail, and in that
        # case we pass None
        is_utf16 = msg & self._UTF16_FLAG
        if is_utf16:
            msg = msg ^ self._UTF16_FLAG
            scan = vk
            key = KeyCode.from_char(six.unichr(scan))
        else:
            try:
                key = self._event_to_key(msg, vk)
            except OSError:
                key = None
 
        if msg in self._PRESS_MESSAGES:
            self.on_press(key)
 
        elif msg in self._RELEASE_MESSAGES:
            self.on_release(key)
 
    # pylint: disable=R0201
    @contextlib.contextmanager
    def _receive(self):
        """An empty context manager; we do not need to fake keyboard events.
        """
        yield
    # pylint: enable=R0201
 
    def _on_notification(self, code, wparam, lparam):
        """Receives ``WM_INPUTLANGCHANGE`` and updates the cached layout.
        """
        if code == self._WM_INPUTLANGCHANGE:
            self._translator.update_layout()
 
    def _event_to_key(self, msg, vk):
        """Converts an :class:`_KBDLLHOOKSTRUCT` to a :class:`KeyCode`.
 
        :param msg: The message received.
 
        :param vk: The virtual key code to convert.
 
        :return: a :class:`pynput.keyboard.KeyCode`
 
        :raises OSError: if the message and data could not be converted
        """
        # If the virtual key code corresponds to a Key value, we prefer that
        if vk in self._SPECIAL_KEYS:
            return self._SPECIAL_KEYS[vk]
        else:
            return KeyCode(**self._translate(
                vk,
                msg in self._PRESS_MESSAGES))
 
    def _translate(self, vk, is_press):
        """Translates a virtual key code to a parameter list passable to
        :class:`pynput.keyboard.KeyCode`.
 
        :param int vk: The virtual key code.
 
        :param bool is_press: Whether this is a press event.
 
        :return: a paramter list to the :class:`pynput.keyboard.KeyCode`
            constructor
        """
        return self._translator(vk, is_press)
 
    def canonical(self, key):
        # If the key has a scan code, and we can find the character for it,
        # return that, otherwise call the super class
        scan = getattr(key, '_scan', None)
        if scan is not None:
            char = self._translator.char_from_scan(scan)
            if char is not None:
                return KeyCode.from_char(char)
 
        return super(Listener, self).canonical(key)


Merci d'avance !