-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from quillcraftsman/capturing
Capturing keeboard
- Loading branch information
Showing
30 changed files
with
543 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[run] | ||
omit = ./testing/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
""" | ||
Manual run module | ||
""" | ||
from quickstart.main import main | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
[MAIN] | ||
ignore-paths=./replay_wizard/package.py | ||
disable= | ||
W0621, # Redefining name outer scope (pytest fixtures) | ||
R0903, # To few public methods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
PyGenesis package | ||
ReplayWizard package | ||
""" | ||
from .main import info | ||
#from .capturing import capture |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
Capturing package | ||
""" | ||
from .capture import capture |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
Capture process module | ||
""" | ||
import json | ||
import pynput | ||
from replay_wizard.models import Sequence | ||
from .keyboard import on_press, on_release | ||
|
||
|
||
def capture(name): | ||
""" | ||
capture user actions | ||
:param name: sequence name | ||
""" | ||
sequence = Sequence( | ||
name=name | ||
) | ||
|
||
def on_press_handler(key): | ||
return on_press(sequence, key) | ||
|
||
def on_release_handler(key): | ||
return on_release(sequence, key) | ||
|
||
with pynput.keyboard.Listener( | ||
on_press=on_press_handler, | ||
on_release=on_release_handler) as listener: | ||
listener.join() | ||
|
||
# save result | ||
result_dict = sequence.model_dump() | ||
file_name = f'{name}.sequence' | ||
with open(file_name, 'w', encoding='utf-8') as f: | ||
json.dump(result_dict, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Capturing Errors | ||
""" | ||
|
||
|
||
class UnknownKeyError(Exception): | ||
""" | ||
Input key was unknown (pynput return None) | ||
""" | ||
|
||
def __str__(self): | ||
""" | ||
Exception to str | ||
""" | ||
return 'Key was unknown (None value)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Capturing keyboard events | ||
""" | ||
from pynput.keyboard import Key, KeyCode | ||
|
||
from replay_wizard.capturing.errors import UnknownKeyError | ||
from replay_wizard.models import Sequence, ActionEnum, Action, Subtypes | ||
|
||
|
||
def key_to_value(key): | ||
""" | ||
Convert key different types to action value | ||
:param key: key to convert | ||
""" | ||
if isinstance(key, Key): | ||
_, name = str(key).split('.') | ||
return name | ||
if isinstance(key, KeyCode): | ||
return key.char | ||
if key is None: | ||
raise UnknownKeyError | ||
raise ValueError(key) | ||
|
||
|
||
def on_key_input(sequence: Sequence, key, action_type: ActionEnum): | ||
""" | ||
Key input | ||
:param sequence: current sequence | ||
:param key: pressed key | ||
:param action: action type | ||
""" | ||
value = key_to_value(key) | ||
if value == 'esc': | ||
return False | ||
|
||
action = Action( | ||
subtype=Subtypes.KEYBOARD, | ||
value=key_to_value(key), | ||
action=action_type, | ||
timedelta=0, | ||
) | ||
sequence.add(action) | ||
return True | ||
|
||
|
||
def on_press(sequence: Sequence, key): | ||
""" | ||
Key was pressed | ||
:param sequence: current sequence | ||
:param key: pressed key | ||
""" | ||
return on_key_input(sequence, key, ActionEnum.PRESS) | ||
|
||
|
||
def on_release(sequence: Sequence, key): | ||
""" | ||
Key was release | ||
:param sequence: current sequence | ||
:param key: pressed key | ||
""" | ||
return on_key_input(sequence, key, ActionEnum.RELEASE) | ||
|
||
# | ||
# | ||
# def on_release(key, f): | ||
# if key == keyboard.Key.esc: | ||
# # Stop listener | ||
# return False | ||
# print('{0} released'.format( | ||
# key)) | ||
# f.write(f'{key.char} released\n') | ||
# | ||
# | ||
# def capture(name): | ||
# file_name = f'{name}.sequence' | ||
# with open(file_name, 'w', encoding='utf-8') as f: | ||
# on_press_handler = lambda key: on_press(key, f) | ||
# on_release_handler = lambda key: on_release(key, f) | ||
# | ||
# with keyboard.Listener( | ||
# on_press=on_press_handler, | ||
# on_release=on_release_handler) as listener: | ||
# listener.join() | ||
# | ||
# # ...or, in a non-blocking fashion: | ||
# # listener = keyboard.Listener( | ||
# # on_press=on_press, | ||
# # on_release=on_release) | ||
# # listener.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +0,0 @@ | ||
""" | ||
Main module | ||
""" | ||
|
||
|
||
def info(): | ||
""" | ||
ReplayWizard info | ||
:return: ReplayWizard usage info | ||
""" | ||
result = ('This is the mock of ReplayWizard package: ' | ||
'https://github.com/quillcraftsman/replay-wizard') | ||
return result | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
Models package | ||
""" | ||
from .action import Action, Subtypes, ActionEnum | ||
from .sequence import Sequence |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Action module | ||
""" | ||
from enum import Enum | ||
from pydantic import BaseModel, ConfigDict | ||
|
||
|
||
class Subtypes(str, Enum): | ||
""" | ||
Action subtype | ||
""" | ||
KEYBOARD = 'keyboard' | ||
MOUSE = 'mouse' | ||
|
||
|
||
class ActionEnum(str, Enum): | ||
""" | ||
press or release | ||
""" | ||
PRESS = 'press' | ||
RELEASE = 'release' | ||
|
||
|
||
class Action(BaseModel): | ||
""" | ||
Action model | ||
""" | ||
model_config = ConfigDict(frozen=True) | ||
|
||
subtype: Subtypes | ||
value: str | ||
action: ActionEnum = ActionEnum.PRESS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Sequence module | ||
""" | ||
from typing import List | ||
from pydantic import BaseModel | ||
from .action import Action | ||
|
||
|
||
class Sequence(BaseModel): | ||
""" | ||
Action sequence | ||
""" | ||
name: str | ||
actions: List[Action] = [] | ||
|
||
def __len__(self): | ||
return len(self.actions) | ||
|
||
def add(self, new_action: Action): | ||
""" | ||
Add action to sequence | ||
:param new_action: Action to add | ||
:return: None | ||
""" | ||
self.actions.append(new_action) | ||
|
||
def __contains__(self, item): | ||
""" | ||
in method | ||
""" | ||
return item in self.actions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
Package info | ||
""" | ||
name = 'replay-wizard' | ||
version = '0.0.1' | ||
version = '0.0.2' | ||
status = '2 - Pre-Alpha' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pynput==1.7.6 | ||
pynput==1.7.6 | ||
pydantic==2.6.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
Pytest fixtures | ||
""" | ||
import pytest | ||
from replay_wizard.models import Sequence | ||
|
||
@pytest.fixture | ||
def empty_sequence(): | ||
""" | ||
Empty sequence fixture | ||
""" | ||
return Sequence( | ||
name='open youtube', | ||
actions=[] | ||
) |
Empty file.
Oops, something went wrong.