title: Python Mouse clicker, can get mouse position but unable to recall it
body: <p>I'm stuck. This is a helper app for my coworkers to refresh a webpage by clicking on the refresh button. 
  What doesn't work so far is grabbing the x,y coordinates (getx,gety) and the GUI locks up after F5 is pressed. I think the issue is when the function on_activate is called the x,y from pyautogui is not available outside the function. maybe? 
  I had to add a function get_position because the listener was causing the GUI to never populate.</p>
  
  <p>I'm still learning python so any help is appreciated. I feel like I'm so close but missing something.</p>
  
  <pre><code>"""
  Created on Mon Dec 16 12:13:06 2019
  Auto clicker for page refresh. Looks for hotkey to grab the mouse location, F5. The button GetPos must be pressed before F5 is available. Then Start Clicking can be pressed and the mouse should click on the x,y coordinates once per delay setting.
  The Pause button will pause clicking.
  """
  from pynput import keyboard
  import pynput
  import PySimpleGUI as sg
  import pyautogui
  import time
  i = 1
  getx=None
  gety=None
  delay =None
  
  #-------------Functions-------------------------------------------------------------------------------------------------
  
  def stop_clicking(): # for pause button
      pass
  
  def on_activate(): # get click location
      print('Global hotkey activated!')
      getx, gety = pyautogui.position()
      positionStr = 'X: ' + str(getx).rjust(4) + ' Y: ' + str(gety).rjust(4)
      print(positionStr)
      end_listener()
      return(getx,gety)
  
  def end_listener(): # end keyboard listener, but does not work
      pynput.keyboard.Listener.stop
      print('end listener')
  
  def ClickMouse(): # click mouse at location loop
      while (i &lt; 20):
          time.sleep(delay)
          pyautogui.click(getx, gety)
          print('clicked'+ getx)
  
  def get_position(): # keyboard listener for hotkey
      hotkey = keyboard.HotKey(
          keyboard.HotKey.parse('&lt;f5&gt;'),
          on_activate)
      with keyboard.Listener(
           on_press=hotkey.press,
          on_release=hotkey.release) as l:
          l.join()
  
  # ----------------  Create PYSimpleGUI Form-----------------------------------------------------------------------------
  sg.ChangeLookAndFeel('LightBlue')
  sg.SetOptions(element_padding=(0, 0))
  
  layout = [[sg.Text('Press F5 to get the mouse position. ')],
            #[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
            [sg.Text('X: ' + str(getx).ljust(4) + ' Y: ' + str(gety).ljust(4), size=(10, 2), font=('Helvetica', 10), justification='center', key='text2')],
            [sg.Text('Delay in Seconds ', font=('Helvetica', 10), justification='center', key='text'),sg.InputText('', size=(3, 1), font=('Helvetica'), key='delay'), sg.Button('Submit',key='submit')],
            [sg.Text('', size=(8, 1), font=('Helvetica', 10), justification='center', key='text3')],
            [sg.ReadButton('Pause', key='pause', button_color=('white', '#001480')),
             sg.ReadButton('Start Clicking', button_color=('white', '#007339'), key='Start'),
             sg.ReadButton('GetPos', button_color=('white', '#007339'), key='getPos'),
             sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]
  
  window = sg.Window('Refresher', layout, no_titlebar=True, auto_size_buttons=False, keep_on_top=True,
                     grab_anywhere=True)
  
  getx,gety = on_activate() # works to get a getx and gety value but calls function to soon, duh
  print(getx) # see if I get a value for getx (debuging)
  
  keys_entered = 'X: ' + str(getx).ljust(4) + ' Y: ' + str(gety).ljust(4) #variable for window refresh below
  
  while True:  # Event Loop
      event, values = window.read()
      if event is None:  # X (close) on the window
          break
      elif event == 'submit':
          delay = values['delay']
      elif event == 'Pause':
          stop_clicking()
      elif event == 'Start':
          ClickMouse()
      elif event == 'Exit':
          exit
      elif event =='getPos':
          get_position()
      window['text2'].update(keys_entered)  # change the window to reflect current key string, so far does not work
  
  window.close()
  </code></pre>
  
  <p>New code using wind32api to replace pyautogui.</p>
  
  <pre><code>import PySimpleGUI as sg
  import time
  import win32api
  import win32con
  import sys
  
  i = 1
  x=None
  y=None
  delay =None
  paused = False
  
  #-------------Functions----------------------------------------------------------
  
  def stop_clicking(): # for pause button
      pass
  
  def on_activate(): # get click location
      print('Global hotkey activated!')
      x, y = win32api.GetCursorPos()
      win32api.SetCursorPos((x, y))
      positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
      print(positionStr)
      time.sleep(2)
  
  def where_is():
      print (x)
      print (y)
  
  def ClickMouse(): # click mouse at location loop
      while True:
          win32api.SetCursorPos((x, y))
          time.sleep(.05)
          win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
          time.sleep(0.05)
          win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
          print('clicked'+ x)
          time.sleep(4)
  
  # ----------------  Create PYSimpleGUI Form--------------------------------------
  sg.ChangeLookAndFeel('LightBlue')
  sg.SetOptions(element_padding=(0, 0))
  
  layout = [[sg.Text('After pressing Get Pos, Press F5 to get the mouse position. ')],
            #[sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')],
            [sg.Text('X: ' + str(x).ljust(4) + ' Y: ' + str(y).ljust(4), size=(20, 1), font=('Helvetica', 10), justification='center', key='text2')],
            [sg.Text('Delay in Seconds ', font=('Helvetica', 10), justification='center', key='text'),sg.InputText('', size=(3, 1), font=('Helvetica'), pad=(2,0) , key='delay'), sg.Button('Submit',key='submit')],
            [sg.Text('', size=(8, 1), font=('Helvetica', 10), justification='center', key='text3')],
            [sg.ReadButton('Pause', key='pause', button_color=('white', '#001480')),
             sg.ReadButton('Start Clicking', button_color=('white', '#007339'), key='Start'),
             sg.ReadButton('Get Pos', key='getpos', button_color=('orange', 'black')),
             sg.Exit(button_color=('white', 'firebrick4'), key='Exit')]]
  
  window = sg.Window('Refresher', layout, no_titlebar=True, auto_size_buttons=False, keep_on_top=True,
                     grab_anywhere=True)
  
  keys_entered = 'X: ' + str(x).ljust(4) + ' Y: ' + str(y).ljust(4) #variable for window refresh below
  
  while True:  # Event Loop
      event, values = window.read()
      if event is None:  # X (close) on the window
          break
      elif event == 'submit':
          delay = values['delay']
      elif event == 'Pause':
          paused =True
          element = window('start')
          element.update(text='Start Clicking')
          stop_clicking()
      elif event == 'Start':
          paused = False
          ClickMouse()
      elif event == 'Exit':
          sys.exit()
  
  
  if win32api.GetAsyncKeyState(0x74):
               print('Global hotkey activated!')
               x, y = win32api.GetCursorPos()
               win32api.SetCursorPos((x, y))
               positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
               print(positionStr)
               time.sleep(2)
  
  window['text2'].update(keys_entered)  # change the window to reflect current key string, so far does not work
  
  window.close()
  </code></pre>