Advertisement
Enlight432

Untitled

Feb 27th, 2025
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. import ctypes
  2. import time
  3. from threading import Timer
  4.  
  5. # دسترسی به توابع ویندوز
  6. user32 = ctypes.windll.user32
  7. kernel32 = ctypes.windll.kernel32
  8.  
  9. # ثابت‌ها برای انواع نشانگرهای سیستمی
  10. SYSTEM_CURSORS = [
  11.     32512,  # IDC_ARROW
  12.     32513,  # IDC_IBEAM
  13.     32514,  # IDC_WAIT
  14.     32515,  # IDC_CROSS
  15.     32516,  # IDC_UPARROW
  16.     32642,  # IDC_SIZENWSE
  17.     32643,  # IDC_SIZENESW
  18.     32644,  # IDC_SIZEWE
  19.     32645,  # IDC_SIZENS
  20.     32646,  # IDC_SIZEALL
  21.     32648,  # IDC_NO
  22.     32649,  # IDC_HAND
  23.     32650,  # IDC_APPSTARTING
  24. ]
  25.  
  26. # دیکشنری برای ذخیره دسته‌های نشانگر
  27. cursors = {}
  28.  
  29. # متغیر برای ردیابی وضعیت نشانگر
  30. visible = True
  31. hidden = False
  32.  
  33. # متغیرهای مربوط به موقعیت ماوس
  34. last_mouse_pos = None
  35. last_move_time = time.time()
  36.  
  37. def system_cursor(cmd):
  38.     global visible, cursors
  39.  
  40.     if cmd == "Reload" or not cursors:
  41.         # بارگذاری و ذخیره نشانگرهای پیش‌فرض و خالی
  42.         for cursor_id in SYSTEM_CURSORS:
  43.             # بارگذاری نشانگر پیش‌فرض
  44.             h_cursor = user32.LoadCursorW(0, cursor_id)
  45.             h_default = user32.CopyImage(h_cursor, 2, 0, 0, 0)
  46.  
  47.             # ایجاد نشانگر خالی (32x32 پیکسل)
  48.             and_mask = b'\xFF' * (32 * 4)  # ماسک AND (همه 1)
  49.             xor_mask = b'\x00' * (32 * 4)  # ماسک XOR (همه 0)
  50.             h_blank = user32.CreateCursor(0, 0, 0, 32, 32,
  51.                                         and_mask, xor_mask)
  52.  
  53.             cursors[cursor_id] = {
  54.                 'default': h_default,
  55.                 'blank': h_blank
  56.             }
  57.  
  58.     # تغییر وضعیت نشانگر
  59.     if cmd == "Show":
  60.         visible = True
  61.     elif cmd == "Hide":
  62.         visible = False
  63.     elif cmd == "Toggle":
  64.         visible = not visible
  65.     else:
  66.         return
  67.  
  68.     # اعمال تغییرات به تمام نشانگرهای سیستمی
  69.     for cursor_id, handles in cursors.items():
  70.         h_cursor = user32.CopyImage(
  71.             handles['default'] if visible else handles['blank'],
  72.             2, 0, 0, 0
  73.         )
  74.         user32.SetSystemCursor(h_cursor, cursor_id)
  75.  
  76. def get_mouse_position():
  77.     # ساختار برای دریافت موقعیت ماوس
  78.     class POINT(ctypes.Structure):
  79.         _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
  80.  
  81.     pt = POINT()
  82.     user32.GetCursorPos(ctypes.byref(pt))
  83.     return (pt.x, pt.y)
  84.  
  85. def check_mouse():
  86.     global hidden, last_mouse_pos, last_move_time
  87.  
  88.     # دریافت موقعیت فعلی ماوس
  89.     current_pos = get_mouse_position()
  90.     current_time = time.time()
  91.  
  92.     # اگر موقعیت ماوس تغییر کرده باشد
  93.     if last_mouse_pos is None:
  94.         last_mouse_pos = current_pos
  95.     elif current_pos != last_mouse_pos:
  96.         last_move_time = current_time
  97.         last_mouse_pos = current_pos
  98.  
  99.     # محاسبه زمان بی‌فعالیتی ماوس (بر اساس آخرین حرکت)
  100.     idle_time = current_time - last_move_time
  101.  
  102.     # اگر ماوس بیش از 2 ثانیه بی‌حرکت باشد و نشانگر قابل مشاهده باشد
  103.     if idle_time > 2 and not hidden:
  104.         system_cursor("Hide")
  105.         hidden = True
  106.     # اگر ماوس حرکت کرده و نشانگر مخفی باشد
  107.     elif idle_time < 2 and hidden:
  108.         system_cursor("Show")
  109.         hidden = False
  110.  
  111.     # تنظیم تایمر برای بررسی بعدی
  112.     Timer(0.1, check_mouse).start()
  113.  
  114. def main():
  115.     # اطمینان از نمایش نشانگر هنگام خروج
  116.     import atexit
  117.     atexit.register(lambda: system_cursor("Show"))
  118.  
  119.     # بارگذاری اولیه نشانگرها
  120.     system_cursor("Reload")
  121.  
  122.     # شروع بررسی ماوس
  123.     check_mouse()
  124.  
  125.     # نگه داشتن برنامه در حال اجرا
  126.     try:
  127.         while True:
  128.             time.sleep(1)
  129.     except KeyboardInterrupt:
  130.         pass
  131.  
  132. if __name__ == "__main__":
  133.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement