如何获取光标的状态?

huangapple go评论148阅读模式
英文:

How to get the state of the cursor?

问题

你可以使用Python 3.8来读取鼠标光标类型,类似于win32gui中的win32gui.GetCursorInfo(),以查找光标的ID。下面是我所指的光标状态:

如何获取光标的状态?

英文:

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:

如何获取光标的状态?

答案1

得分: 1

Please find the code below.

reference:
https://www.iditect.com/how-to/10284849.html, but I modified it a little bit:

  1. from win32con import IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, \
  2. IDC_HELP, IDC_IBEAM, IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, \
  3. IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
  4. from win32gui import LoadCursor, GetCursorInfo
  5. def get_current_cursor():
  6. curr_cursor_handle = GetCursorInfo()[1]
  7. return Cursor.from_handle(curr_cursor_handle)
  8. class Cursor(object):
  9. @classmethod
  10. def from_handle(cls, handle):
  11. for cursor in DEFAULT_CURSORS:
  12. if cursor[1].handle == handle:
  13. return cursor[0] #DEFAULT_CURSORS.index(cursor) , Cursor.__init__
  14. return cls(handle=handle)
  15. def __init__(self, cursor_type=None, handle=None):
  16. if handle is None:
  17. handle = LoadCursor(0, cursor_type)
  18. self.type = cursor_type
  19. self.handle = handle
  20. DEFAULT_CURSORS = (('APPSTARTING',Cursor(IDC_APPSTARTING)), ('ARROW',Cursor(IDC_ARROW)), ('CROSS',Cursor(IDC_CROSS)), ('HAND',Cursor(IDC_HAND)), \
  21. ('HELP',Cursor(IDC_HELP)), ('IBEAM',Cursor(IDC_IBEAM)), ('ICON',Cursor(IDC_ICON)), ('NO',Cursor(IDC_NO)), ('SIZE',Cursor(IDC_SIZE)),\
  22. ('SIZEALL', Cursor(IDC_SIZEALL)),('SIZENESW', Cursor(IDC_SIZENESW)), ('SIZENS',Cursor(IDC_SIZENS)), ('SIZENWSE',Cursor(IDC_SIZENWSE)),\
  23. ('SIZEWE', Cursor(IDC_SIZEWE)), ('UPARROW', Cursor(IDC_UPARROW)), ('WAIT',Cursor(IDC_WAIT)))
  24. while(True):
  25. print(get_current_cursor())
英文:

Please find the code below.

reference:
https://www.iditect.com/how-to/10284849.html, but I modified it a little bit:

  1. from win32con import IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, \
  2. IDC_HELP, IDC_IBEAM, IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, \
  3. IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
  4. from win32gui import LoadCursor, GetCursorInfo
  5. def get_current_cursor():
  6. curr_cursor_handle = GetCursorInfo()[1]
  7. return Cursor.from_handle(curr_cursor_handle)
  8. class Cursor(object):
  9. @classmethod
  10. def from_handle(cls, handle):
  11. for cursor in DEFAULT_CURSORS:
  12. if cursor[1].handle == handle:
  13. return cursor[0] #DEFAULT_CURSORS.index(cursor) , Cursor.__init__
  14. return cls(handle=handle)
  15. def __init__(self, cursor_type=None, handle=None):
  16. if handle is None:
  17. handle = LoadCursor(0, cursor_type)
  18. self.type = cursor_type
  19. self.handle = handle
  20. DEFAULT_CURSORS = (('APPSTARTING',Cursor(IDC_APPSTARTING)), ('ARROW',Cursor(IDC_ARROW)), ('CROSS',Cursor(IDC_CROSS)), ('HAND',Cursor(IDC_HAND)), \
  21. ('HELP',Cursor(IDC_HELP)), ('IBEAM',Cursor(IDC_IBEAM)), ('ICON',Cursor(IDC_ICON)), ('NO',Cursor(IDC_NO)), ('SIZE',Cursor(IDC_SIZE)),\
  22. ('SIZEALL', Cursor(IDC_SIZEALL)),('SIZENESW', Cursor(IDC_SIZENESW)), ('SIZENS',Cursor(IDC_SIZENS)), ('SIZENWSE',Cursor(IDC_SIZENWSE)),\
  23. ('SIZEWE', Cursor(IDC_SIZEWE)), ('UPARROW', Cursor(IDC_UPARROW)), ('WAIT',Cursor(IDC_WAIT)))
  24. while(True):
  25. print(get_current_cursor())

答案2

得分: 0

我已经进行了一些改进,以使其更加简化。

英文:

I've made some improvements to be more streamlined

  1. from win32con import IDC_APPSTARTING, IDC_ARROW, IDC_CROSS, IDC_HAND, \
  2. IDC_HELP, IDC_IBEAM, IDC_ICON, IDC_NO, IDC_SIZE, IDC_SIZEALL, \
  3. IDC_SIZENESW, IDC_SIZENS, IDC_SIZENWSE, IDC_SIZEWE, IDC_UPARROW, IDC_WAIT
  4. from win32gui import LoadCursor, GetCursorInfo
  5. DEFAULT_CURSORS = {
  6. LoadCursor(0, IDC_APPSTARTING): 'appStarting',
  7. LoadCursor(0, IDC_ARROW): 'Arrow', LoadCursor(0, IDC_CROSS): 'Cross',
  8. LoadCursor(0, IDC_HAND): 'Hand', LoadCursor(0, IDC_HELP): 'Help',
  9. LoadCursor(0, IDC_IBEAM): 'IBeam', LoadCursor(0, IDC_ICON): 'ICon',
  10. LoadCursor(0, IDC_NO): 'No', LoadCursor(0, IDC_SIZE): 'Size',
  11. LoadCursor(0, IDC_SIZEALL): 'sizeAll',
  12. LoadCursor(0, IDC_SIZENESW): 'sizeNesw',
  13. LoadCursor(0, IDC_SIZENS): 'sizeNs',
  14. LoadCursor(0, IDC_SIZENWSE): 'sizeNwse',
  15. LoadCursor(0, IDC_SIZEWE): 'sizeWe',
  16. LoadCursor(0, IDC_UPARROW): 'upArrow',
  17. LoadCursor(0, IDC_WAIT): 'Wait',
  18. }
  19. def get_current_cursor():
  20. curr_cursor_handle = GetCursorInfo()[1]
  21. res = DEFAULT_CURSORS.get(curr_cursor_handle, 'None')
  22. return res
  23. if __name__ == '__main__':
  24. while True:
  25. get_current_cursor()

huangapple
  • 本文由 发表于 2020年1月6日 16:33:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定