Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ctypes
- import time
- from threading import Timer
- # دسترسی به توابع ویندوز
- user32 = ctypes.windll.user32
- kernel32 = ctypes.windll.kernel32
- # ثابتها برای انواع نشانگرهای سیستمی
- SYSTEM_CURSORS = [
- 32512, # IDC_ARROW
- 32513, # IDC_IBEAM
- 32514, # IDC_WAIT
- 32515, # IDC_CROSS
- 32516, # IDC_UPARROW
- 32642, # IDC_SIZENWSE
- 32643, # IDC_SIZENESW
- 32644, # IDC_SIZEWE
- 32645, # IDC_SIZENS
- 32646, # IDC_SIZEALL
- 32648, # IDC_NO
- 32649, # IDC_HAND
- 32650, # IDC_APPSTARTING
- ]
- # دیکشنری برای ذخیره دستههای نشانگر
- cursors = {}
- # متغیر برای ردیابی وضعیت نشانگر
- visible = True
- hidden = False
- # متغیرهای مربوط به موقعیت ماوس
- last_mouse_pos = None
- last_move_time = time.time()
- def system_cursor(cmd):
- global visible, cursors
- if cmd == "Reload" or not cursors:
- # بارگذاری و ذخیره نشانگرهای پیشفرض و خالی
- for cursor_id in SYSTEM_CURSORS:
- # بارگذاری نشانگر پیشفرض
- h_cursor = user32.LoadCursorW(0, cursor_id)
- h_default = user32.CopyImage(h_cursor, 2, 0, 0, 0)
- # ایجاد نشانگر خالی (32x32 پیکسل)
- and_mask = b'\xFF' * (32 * 4) # ماسک AND (همه 1)
- xor_mask = b'\x00' * (32 * 4) # ماسک XOR (همه 0)
- h_blank = user32.CreateCursor(0, 0, 0, 32, 32,
- and_mask, xor_mask)
- cursors[cursor_id] = {
- 'default': h_default,
- 'blank': h_blank
- }
- # تغییر وضعیت نشانگر
- if cmd == "Show":
- visible = True
- elif cmd == "Hide":
- visible = False
- elif cmd == "Toggle":
- visible = not visible
- else:
- return
- # اعمال تغییرات به تمام نشانگرهای سیستمی
- for cursor_id, handles in cursors.items():
- h_cursor = user32.CopyImage(
- handles['default'] if visible else handles['blank'],
- 2, 0, 0, 0
- )
- user32.SetSystemCursor(h_cursor, cursor_id)
- def get_mouse_position():
- # ساختار برای دریافت موقعیت ماوس
- class POINT(ctypes.Structure):
- _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
- pt = POINT()
- user32.GetCursorPos(ctypes.byref(pt))
- return (pt.x, pt.y)
- def check_mouse():
- global hidden, last_mouse_pos, last_move_time
- # دریافت موقعیت فعلی ماوس
- current_pos = get_mouse_position()
- current_time = time.time()
- # اگر موقعیت ماوس تغییر کرده باشد
- if last_mouse_pos is None:
- last_mouse_pos = current_pos
- elif current_pos != last_mouse_pos:
- last_move_time = current_time
- last_mouse_pos = current_pos
- # محاسبه زمان بیفعالیتی ماوس (بر اساس آخرین حرکت)
- idle_time = current_time - last_move_time
- # اگر ماوس بیش از 2 ثانیه بیحرکت باشد و نشانگر قابل مشاهده باشد
- if idle_time > 2 and not hidden:
- system_cursor("Hide")
- hidden = True
- # اگر ماوس حرکت کرده و نشانگر مخفی باشد
- elif idle_time < 2 and hidden:
- system_cursor("Show")
- hidden = False
- # تنظیم تایمر برای بررسی بعدی
- Timer(0.1, check_mouse).start()
- def main():
- # اطمینان از نمایش نشانگر هنگام خروج
- import atexit
- atexit.register(lambda: system_cursor("Show"))
- # بارگذاری اولیه نشانگرها
- system_cursor("Reload")
- # شروع بررسی ماوس
- check_mouse()
- # نگه داشتن برنامه در حال اجرا
- try:
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- pass
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement