I was wondering if there was any way in Python 3.8 to read the cursor type like how win32gui could do with win32gui.GetCursorInfo(), Finding the id of the cursor. Below is what I mean by cursor state:
Asked
Active
Viewed 1,586 times
1
Tomerikoo
- 18,379
- 16
- 47
- 61
Alex Dalton
- 55
- 3
-
1What's wrong with the way you already know? – DeepSpace Jan 06 '20 at 08:36
-
Python 3.8 Doesn't support it – Alex Dalton Jan 07 '20 at 12:27
-
https://stackoverflow.com/questions/58631512/pywin32-and-python-3-8-0 – DeepSpace Jan 07 '20 at 12:40
2 Answers
1
Please find the code below.
reference: https://www.iditect.com/how-to/10284849.html, but I modified it a little bit:
from win32con import IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, \
IDC_HELP, IDC_IBEAM, IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, \
IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
from win32gui import LoadCursor, GetCursorInfo
def get_current_cursor():
curr_cursor_handle = GetCursorInfo()[1]
return Cursor.from_handle(curr_cursor_handle)
class Cursor(object):
@classmethod
def from_handle(cls, handle):
for cursor in DEFAULT_CURSORS:
if cursor[1].handle == handle:
return cursor[0] #DEFAULT_CURSORS.index(cursor) , Cursor.__init__
return cls(handle=handle)
def __init__(self, cursor_type=None, handle=None):
if handle is None:
handle = LoadCursor(0, cursor_type)
self.type = cursor_type
self.handle = handle
DEFAULT_CURSORS = (('APPSTARTING',Cursor(IDC_APPSTARTING)), ('ARROW',Cursor(IDC_ARROW)), ('CROSS',Cursor(IDC_CROSS)), ('HAND',Cursor(IDC_HAND)), \
('HELP',Cursor(IDC_HELP)), ('IBEAM',Cursor(IDC_IBEAM)), ('ICON',Cursor(IDC_ICON)), ('NO',Cursor(IDC_NO)), ('SIZE',Cursor(IDC_SIZE)),\
('SIZEALL', Cursor(IDC_SIZEALL)),('SIZENESW', Cursor(IDC_SIZENESW)), ('SIZENS',Cursor(IDC_SIZENS)), ('SIZENWSE',Cursor(IDC_SIZENWSE)),\
('SIZEWE', Cursor(IDC_SIZEWE)), ('UPARROW', Cursor(IDC_UPARROW)), ('WAIT',Cursor(IDC_WAIT)))
while(True):
print(get_current_cursor())
0
I've made some improvements to be more streamlined
from win32con import IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, \
IDC_HELP, IDC_IBEAM, IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, \
IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
from win32gui import LoadCursor, GetCursorInfo
DEFAULT_CURSORS = {
LoadCursor(0, IDC_APPSTARTING): 'appStarting',
LoadCursor(0, IDC_ARROW): 'Arrow', LoadCursor(0, IDC_CROSS): 'Cross',
LoadCursor(0, IDC_HAND): 'Hand', LoadCursor(0, IDC_HELP): 'Help',
LoadCursor(0, IDC_IBEAM): 'IBeam', LoadCursor(0, IDC_ICON): 'ICon',
LoadCursor(0, IDC_NO): 'No', LoadCursor(0, IDC_SIZE): 'Size',
LoadCursor(0, IDC_SIZEALL): 'sizeAll',
LoadCursor(0, IDC_SIZENESW): 'sizeNesw',
LoadCursor(0, IDC_SIZENS): 'sizeNs',
LoadCursor(0, IDC_SIZENWSE): 'sizeNwse',
LoadCursor(0, IDC_SIZEWE): 'sizeWe',
LoadCursor(0, IDC_UPARROW): 'upArrow',
LoadCursor(0, IDC_WAIT): 'Wait',
}
def get_current_cursor():
curr_cursor_handle = GetCursorInfo()[1]
res = DEFAULT_CURSORS.get(curr_cursor_handle, 'None')
return res
if __name__ == '__main__':
while True:
get_current_cursor()
jiaozhu
- 1
- 3
