setfocus.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. '''Created 16 Jan 2023 Levi'''
  2. import sys
  3. import traceback
  4. # import win32gui as wgui
  5. # import win32process as wproc
  6. # import win32api as wapi
  7. import keyboard as kb
  8. import pyautogui as gui
  9. import pywinctl as gw
  10. wnds = {'mzk': 'NextUp ERP - MOZAIK CONSULTING SRL',
  11. 'alp': 'NextUp ERP - ALPA SOLUTION SRL',
  12. 'east': 'NextUp ERP - ASOCIATIA CULTURALA GITAR EAST',
  13. 'srv': 'NextUp ERP [Admirustrare Server]',
  14. 'ant': 'utilizator: Master',
  15. 'dec': 'utilizator: Declaratii'}
  16. def pretty_exc(exc):
  17. exc_type, _exc_obj, exc_tb = sys.exc_info()
  18. # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  19. for tb in list(traceback.format_exception(exc_type, _exc_obj, exc_tb)):
  20. print(tb)
  21. def set_focus(*argv):
  22. # if not argv:
  23. window_name = gui.prompt(text="Enter window name", title='wnd_name')
  24. window_name = window_name.lower()
  25. # else:
  26. # window_name = wnds[argv[0]]
  27. # handle = wgui.FindWindow(None, wnds[window_name])
  28. try:
  29. handle = gw.getWindowsWithTitle(wnds[window_name])
  30. except KeyError as exc_:
  31. pretty_exc(exc_)
  32. print()
  33. # print("Window `{0:s}` handle: 0x{1:016X}".format(wnds[window_name], handle))
  34. # if not handle:
  35. # print("Invalid window handle")
  36. # return
  37. # remote_thread, _ = wproc.GetWindowThreadProcessId(handle)
  38. # wproc.AttachThreadInput(wapi.GetCurrentThreadId(), remote_thread, True)
  39. # wgui.SetFocus(handle)
  40. # wgui.SetForegroundWindow(handle)
  41. try:
  42. handle[0].activate()
  43. except Exception as exc_:
  44. pretty_exc(exc_)
  45. def main():
  46. '''setfocus'''
  47. kb.register_hotkey('control+alt+w', set_focus, args=()) # set_focus
  48. # kb.wait()
  49. if __name__ == "__main__":
  50. # set_focus()
  51. # print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
  52. main()
  53. # kb.wait()