-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowinfo.py
More file actions
executable file
·113 lines (97 loc) · 3.36 KB
/
Copy pathwindowinfo.py
File metadata and controls
executable file
·113 lines (97 loc) · 3.36 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
"""Extracts the threadinfo of the current thread. Can see if a menu
or popup is active
hasMenuOpen() gives true if menu (File, Edit, ...) is open or a right click
context menu is open.
hasPopupOpen() give true if a right click (context) menu is open.
getWindowFlags gives (as a tuple) the flags that are set in the guithreadinfo.flags variable.
Used for grammar kaiser_dictation.py in conjunction with unimacro grammars
(qh.antenna.nl/unimacro). Quintijn Hoogenboom, december 2009
"""
from ctypes import *
import types
user32 = windll.user32
kernel32 = windll.kernel32
import time
class RECT(Structure):
_fields_ = [
("left", c_ulong),
("top", c_ulong),
("right", c_ulong),
("bottom", c_ulong)
]
class GUITHREADINFO(Structure):
_fields_ = [
("cbSize", c_ulong),
("flags", c_ulong),
("hwndActive", c_ulong),
("hwndFocus", c_ulong),
("hwndCapture", c_ulong),
("hwndMenuOwner", c_ulong),
("hwndMoveSize", c_ulong),
("hwndCaret", c_ulong),
("rcCaret", RECT)
]
def moveCursorInCurrentWindow(x, y):
# Find the focussed window.
guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
focussedWindow = guiThreadInfo.hwndFocus
# Find the screen position of the window.
windowRect = RECT()
user32.GetWindowRect(focussedWindow, byref(windowRect))
# Finally, move the cursor relative to the window.
user32.SetCursorPos(windowRect.left + x, windowRect.top + y)
def hasMenuOpen():
guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
flags = guiThreadInfo.flags
menuOpen = 4
return (flags & menuOpen) == menuOpen
def hasPopupOpen():
guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
flags = guiThreadInfo.flags
popupOpen = 16
return (flags & popupOpen == popupOpen)
def getWindowFlags():
guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
return flagsToTuple(guiThreadInfo.flags)
def flagsToTuple(flags):
"""convert a flags int into a set of flags
"""
if flags == None:
return ()
elif flags == 0:
return ()
Flags = []
if type(flags) in (types.IntType, types.LongType):
if type(flags) == types.IntType: n = 32
elif type(flags) == types.LongType: n = 64
else:
raise ValueError('type should be "int" or "long" here, not: %s'% type(flags))
if flags:
for i in range(n):
if flags & (1<<i):
Flags.append(i)
else:
pass # flags == 0
elif type(flags) in (types.TupleType, types.ListType):
Flags = flags
else:
print 'type flags: %s'% type(flags)
Flags = flags
return tuple(Flags)
#GUI_CARETBLINKING = 1
#GUI_INMOVESIZE = 2
#GUI_INMENUMODE = 4
#GUI_SYSTEMMENUMODE = 8
#GUI_POPUPMENUMODE = 16
if __name__ == '__main__':
# Quick test.
#moveCursorInCurrentWindow(200, 200)
# should return False:
time.sleep(1)
print 'hasMenuOpen: %s'% hasMenuOpen()
print 'hasPopupOpen: %s'% hasPopupOpen()
print 'flags: %s'% `getWindowFlags()`