-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrackwindow.py
More file actions
185 lines (143 loc) · 6.06 KB
/
trackwindow.py
File metadata and controls
185 lines (143 loc) · 6.06 KB
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
# using pywin32 for constants and ctypes for everything else seems a little
import win32con
import win32gui
import win32api
import py_win_keyboard_layout
import sys
import ctypes
import ctypes.wintypes
user32 = ctypes.windll.user32
ole32 = ctypes.windll.ole32
kernel32 = ctypes.windll.kernel32
WinEventProcType = ctypes.WINFUNCTYPE(
None,
ctypes.wintypes.HANDLE,
ctypes.wintypes.DWORD,
ctypes.wintypes.HWND,
ctypes.wintypes.LONG,
ctypes.wintypes.LONG,
ctypes.wintypes.DWORD,
ctypes.wintypes.DWORD
)
# The types of events we want to listen for, and the names we'll use for
# them in the log output. Pick from
# http://msdn.microsoft.com/en-us/library/windows/desktop/dd318066(v=vs.85).aspx
eventTypes = {
# win32con.EVENT_SYSTEM_FOREGROUND: "Foreground",
win32con.EVENT_OBJECT_FOCUS: "Focus",
}
# limited information would be sufficient, but our platform doesn't have it.
processFlag = getattr(win32con, 'PROCESS_QUERY_LIMITED_INFORMATION',
win32con.PROCESS_QUERY_INFORMATION)
threadFlag = getattr(win32con, 'THREAD_QUERY_LIMITED_INFORMATION',
win32con.THREAD_QUERY_INFORMATION)
# store last event time for displaying time between events
lastTime = 0
def log(msg):
print(msg)
def logError(msg):
sys.stdout.write(msg + '\n')
def getProcessID(dwEventThread, hwnd):
# It's possible to have a window we can get a PID out of when the thread
# isn't accessible, but it's also possible to get called with no window,
# so we have two approaches.
hThread = kernel32.OpenThread(threadFlag, 0, dwEventThread)
if hThread:
try:
processID = kernel32.GetProcessIdOfThread(hThread)
if not processID:
logError("Couldn't get process for thread %s: %s" %
(hThread, ctypes.WinError()))
finally:
kernel32.CloseHandle(hThread)
else:
errors = ["No thread handle for %s: %s" %
(dwEventThread, ctypes.WinError(),)]
if hwnd:
processID = ctypes.wintypes.DWORD()
threadID = user32.GetWindowThreadProcessId(
hwnd, ctypes.byref(processID))
if threadID != dwEventThread:
logError("Window thread != event thread? %s != %s" %
(threadID, dwEventThread))
if processID:
processID = processID.value
else:
errors.append(
"GetWindowThreadProcessID(%s) didn't work either: %s" % (
hwnd, ctypes.WinError()))
processID = None
else:
processID = None
if not processID:
for err in errors:
logError(err)
return processID
def getProcessFilename(processID):
hProcess = kernel32.OpenProcess(processFlag, 0, processID)
if not hProcess:
logError("OpenProcess(%s) failed: %s" % (processID, ctypes.WinError()))
return None
try:
filenameBufferSize = ctypes.wintypes.DWORD(4096)
filename = ctypes.create_unicode_buffer(filenameBufferSize.value)
kernel32.QueryFullProcessImageNameW(hProcess, 0, ctypes.byref(filename),
ctypes.byref(filenameBufferSize))
return filename.value
finally:
kernel32.CloseHandle(hProcess)
def callback(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread,
dwmsEventTime):
set_keyboard_layout()
# lastTime = dwmsEventTime
def set_keyboard_layout():
w = win32gui
window_title = w.GetWindowText(w.GetForegroundWindow())
print("Title: " + window_title)
if 'whatsapp' in window_title.lower():
py_win_keyboard_layout.change_foreground_window_keyboard_layout(-264436723) # to switch to Heb
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " Hebrew, whatsapp")
elif any("\u0590" <= c <= "\u05EA" for c in window_title):
py_win_keyboard_layout.change_foreground_window_keyboard_layout(-264436723) # to switch to Heb
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " Hebrew, heb")
elif 'visual studio code' in window_title.lower():
py_win_keyboard_layout.change_foreground_window_keyboard_layout(67699721) # to switch to en
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " English, vsc")
elif 'chrome' in window_title.lower():
py_win_keyboard_layout.change_foreground_window_keyboard_layout(67699721) # to switch to en
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " English, chrome")
elif 'gitkraken' in window_title.lower():
py_win_keyboard_layout.change_foreground_window_keyboard_layout(67699721) # to switch to en
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " English, git")
elif 'cmd' in window_title.lower():
py_win_keyboard_layout.change_foreground_window_keyboard_layout(67699721) # to switch to en
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " English, cmd")
elif 'ssh' in window_title.lower():
py_win_keyboard_layout.change_foreground_window_keyboard_layout(67699721) # to switch to en
print("Keyboard changed " + str(win32api.GetKeyboardLayout()) + " English, ssh")
def setHook(WinEventProc, eventType):
return user32.SetWinEventHook(
eventType,
eventType,
0,
WinEventProc,
0,
0,
win32con.WINEVENT_OUTOFCONTEXT
)
def track_window():
ole32.CoInitialize(0)
WinEventProc = WinEventProcType(callback)
user32.SetWinEventHook.restype = ctypes.wintypes.HANDLE
hookIDs = [setHook(WinEventProc, et) for et in eventTypes.keys()]
if not any(hookIDs):
print('SetWinEventHook failed')
sys.exit(1)
msg = ctypes.wintypes.MSG()
while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
user32.TranslateMessageW(msg)
user32.DispatchMessageW(msg)
for hookID in hookIDs:
user32.UnhookWinEvent(hookID)
ole32.CoUninitialize()
# if __name__ == '__main__':