복붙노트

[PYTHON] 파이썬에서 커서 위치 찾기

PYTHON

파이썬에서 커서 위치 찾기

Windows에서 표준 Python 라이브러리를 사용하여 전체 커서 위치를 가져올 수 있습니까?

해결법

  1. ==============================

    1.

    win32gui.GetCursorPos(point)
    

    화면 좌표에서 커서의 위치를 ​​검색합니다.-point = (x, y)

    flags, hcursor, (x,y) = win32gui.GetCursorInfo()
    

    전역 커서에 대한 정보를 검색합니다.

    모래밭:

    나는 당신이 python win32 API 바인딩이나 pywin32를 사용하고있을 것이라고 가정하고있다.

  2. ==============================

    2.표준 ctypes 라이브러리를 사용하면 타사 모듈없이 화면상의 마우스 좌표를 얻을 수 있습니다.

    표준 ctypes 라이브러리를 사용하면 타사 모듈없이 화면상의 마우스 좌표를 얻을 수 있습니다.

    from ctypes import windll, Structure, c_long, byref
    
    
    class POINT(Structure):
        _fields_ = [("x", c_long), ("y", c_long)]
    
    
    
    def queryMousePosition():
        pt = POINT()
        windll.user32.GetCursorPos(byref(pt))
        return { "x": pt.x, "y": pt.y}
    
    
    pos = queryMousePosition()
    print(pos)
    

    이 코드는 여기에서 찾은 예제에서 가져온 것임을 언급해야합니다. 따라서이 솔루션에 대한 크레딧은 Nullege.com으로 이동합니다.

  3. ==============================

    3.이 함수는 Windows 전용이지만 표준 Python 라이브러리에서는 이러한 함수를 찾을 수 없습니다. 그러나 ActiveState Python을 사용하거나 표준 Python Windows 설치에 win32api 모듈을 설치하면 다음을 사용할 수 있습니다.

    이 함수는 Windows 전용이지만 표준 Python 라이브러리에서는 이러한 함수를 찾을 수 없습니다. 그러나 ActiveState Python을 사용하거나 표준 Python Windows 설치에 win32api 모듈을 설치하면 다음을 사용할 수 있습니다.

    x, y = win32api.GetCursorPos()
    
  4. ==============================

    4.비표준 라이브러리에 의존하지 않는 방법을 찾았습니다!

    비표준 라이브러리에 의존하지 않는 방법을 찾았습니다!

    Tkinter에서 이것을 발견했습니다.

    self.winfo_pointerxy()
    
  5. ==============================

    5.Tkinter를 설치하십시오. win32api를 Windows 전용 솔루션으로 포함 시켰습니다.

    Tkinter를 설치하십시오. win32api를 Windows 전용 솔루션으로 포함 시켰습니다.

    #!/usr/bin/env python
    
    """Get the current mouse position."""
    
    import logging
    import sys
    
    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.DEBUG,
                        stream=sys.stdout)
    
    
    def get_mouse_position():
        """
        Get the current position of the mouse.
    
        Returns
        -------
        dict :
            With keys 'x' and 'y'
        """
        mouse_position = None
        import sys
        if sys.platform in ['linux', 'linux2']:
            pass
        elif sys.platform == 'Windows':
            try:
                import win32api
            except ImportError:
                logging.info("win32api not installed")
                win32api = None
            if win32api is not None:
                x, y = win32api.GetCursorPos()
                mouse_position = {'x': x, 'y': y}
        elif sys.platform == 'Mac':
            pass
        else:
            try:
                import Tkinter  # Tkinter could be supported by all systems
            except ImportError:
                logging.info("Tkinter not installed")
                Tkinter = None
            if Tkinter is not None:
                p = Tkinter.Tk()
                x, y = p.winfo_pointerxy()
                mouse_position = {'x': x, 'y': y}
            print("sys.platform={platform} is unknown. Please report."
                  .format(platform=sys.platform))
            print(sys.version)
        return mouse_position
    
    print(get_mouse_position())
    
  6. from https://stackoverflow.com/questions/3698635/getting-cursor-position-in-python by cc-by-sa and MIT license