FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Macro/SystemScripts/key_listener.py at master · softpython2884/Macro · GitHub
softpython2884
/
Macro
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Macro
/
SystemScripts
/
key_listener.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
103 lines (88 loc) · 3.32 KB
Breadcrumbs
Macro
/
SystemScripts
/
key_listener.py
Copy path
File metadata and controls
103 lines (88 loc) · 3.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
time
import
threading
import
configparser
import
os
import
psutil
import
keyboard
try
:
from
inputs
import
get_gamepad
,
UnpluggedError
except
ImportError
:
get_gamepad
=
None
import
pyautogui
# Lire la config
config
=
configparser
.
ConfigParser
()
# Path relative to this script's location
script_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
))
config_path
=
os
.
path
.
join
(
script_dir
,
'config.ini'
)
config
.
read
(
config_path
)
browser_process
=
config
.
get
(
"general"
,
"browser"
,
fallback
=
"chrome.exe"
).
strip
()
# État d'appui
key_hold_time
=
{}
HOLD_THRESHOLD
=
3
# secondes
def
alt_tab
():
pyautogui
.
keyDown
(
"alt"
)
pyautogui
.
press
(
"tab"
)
pyautogui
.
keyUp
(
"alt"
)
def
kill_browser
():
print
(
f"[INFO] Attempting to close process:
{
browser_process
}
"
)
for
proc
in
psutil
.
process_iter
([
'pid'
,
'name'
]):
if
proc
.
info
[
'name'
]
and
proc
.
info
[
'name'
].
lower
()
==
browser_process
.
lower
():
try
:
psutil
.
Process
(
proc
.
info
[
'pid'
]).
terminate
()
print
(
f"[INFO] Terminated:
{
proc
.
info
[
'name'
]
}
"
)
except
Exception
as
e
:
print
(
f"[ERROR] Could not terminate
{
proc
.
info
[
'name'
]
}
:
{
e
}
"
)
def
handle_press
(
key_name
):
if
key_name
not
in
key_hold_time
:
key_hold_time
[
key_name
]
=
time
.
time
()
print
(
f"[INFO]
{
key_name
}
pressed. Hold for
{
HOLD_THRESHOLD
}
s to kill browser."
)
def
handle_release
(
key_name
):
if
key_name
in
key_hold_time
:
duration
=
time
.
time
()
-
key_hold_time
[
key_name
]
del
key_hold_time
[
key_name
]
print
(
f"[INFO]
{
key_name
}
released after
{
duration
:.2f
}
s."
)
if
duration
>=
HOLD_THRESHOLD
:
kill_browser
()
else
:
alt_tab
()
# Thread pour touche F9
def
monitor_keyboard
():
keyboard
.
on_press_key
(
"f9"
,
lambda
_
:
handle_press
(
"f9"
))
keyboard
.
on_release_key
(
"f9"
,
lambda
_
:
handle_release
(
"f9"
))
print
(
"[INFO] Keyboard listener for F9 is active."
)
keyboard
.
wait
()
# Thread pour bouton Xbox
def
monitor_gamepad
():
if
get_gamepad
is
None
:
print
(
"[WARN] 'inputs' library not found. Gamepad listener disabled. Run 'pip install inputs'."
)
return
print
(
"[INFO] Gamepad listener for Xbox button is active."
)
pressed
=
False
while
True
:
try
:
events
=
get_gamepad
()
for
event
in
events
:
if
event
.
code
==
"BTN_MODE"
:
if
event
.
state
==
1
and
not
pressed
:
handle_press
(
"xbox"
)
pressed
=
True
elif
event
.
state
==
0
and
pressed
:
handle_release
(
"xbox"
)
pressed
=
False
except
UnpluggedError
:
print
(
"[INFO] Gamepad unplugged. Waiting for connection..."
)
time
.
sleep
(
5
)
except
Exception
as
e
:
print
(
f"[ERROR Gamepad] An unexpected error occurred:
{
e
}
"
)
time
.
sleep
(
5
)
if
__name__
==
"__main__"
:
print
(
"--- Macro System Listener v2 ---"
)
print
(
f"Target browser process:
{
browser_process
}
"
)
print
(
"Action: Tap F9/Xbox for Alt+Tab, Hold for 3s to kill browser."
)
threading
.
Thread
(
target
=
monitor_keyboard
,
daemon
=
True
).
start
()
threading
.
Thread
(
target
=
monitor_gamepad
,
daemon
=
True
).
start
()
try
:
while
True
:
time
.
sleep
(
1
)
except
KeyboardInterrupt
:
print
(
"
\n
Script stopped by user."
)
Back
|
FazBrowse Home
|
New Git URL