开发者

Simple Hotkey Script in Python - How to set a global hotkey to send a string of text?

开发者 https://www.devze.com 2023-02-18 17:04 出处:网络
I was wondering how I could use wxPython along with the win32apis to create a simple script that will activate a window (if it is not already active) with a certain title and output text (keystrokes).

I was wondering how I could use wxPython along with the win32apis to create a simple script that will activate a window (if it is not already active) with a certain title and output text (keystrokes). One possible application for this would be keyboard shortcuts in gaming. I have read up on the wxPython RegisterHotKey(), but- as an amateur Python programmer- it is unclear to me.

The basic structure of the script will be:

  1. Define the hotkey (something like win+F_)
  2. Watch for the hotkey keystroke
  3. See if the desired window (title) is already active, and activate it if it isn't
  4. Simulate the typing of some text

I know there are simpler methods to accomplish this (such as AutoHotkey), but I feel more comfortable using something I have written myself an开发者_StackOverflow社区d have taken an interest in Python.

Thanks!

For the record, I am using Python 2.7 on Windows 7 AMD64, though I doubt that the interpreter version/platform/architecture makes much of a difference here.


Are you talking about activating a window that you created in wx or a separate application, like notepad? If it's with wx, then it's trivial. You'd just use Raise() to bring whatever frame you need into focus. You would probably use PubSub or PostEvent to let the sub-frame know that it needs to Raise.

If you're talking about notepad, then things get much stickier. Here's an ugly hack I created based on some stuff I got from various locations on the web and the PyWin32 mailing list:

def windowEnumerationHandler(self, hwnd, resultList):
    '''
    This is a handler to be passed to win32gui.EnumWindows() to generate
    a list of (window handle, window text) tuples.
    '''

    resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def bringToFront(self, windowText):
    '''
    Method to look for an open window that has a title that
    matches the passed in text. If found, it will proceed to
    attempt to make that window the Foreground Window.
    '''
    secondsPassed = 0
    while secondsPassed <= 5:
        # sleep one second to give the window time to appear
        wx.Sleep(1)

        print 'bringing to front'
        topWindows = []
        # pass in an empty list to be filled
        # somehow this call returns the list with the same variable name
        win32gui.EnumWindows(self.windowEnumerationHandler, topWindows)
        print len(topWindows)
        # loop through windows and find the one we want
        for i in topWindows:
            if windowText in i[1]:
                print i[1]
                win32gui.ShowWindow(i[0],5)
                win32gui.SetForegroundWindow(i[0])
        # loop for 5-10 seconds, then break or raise
        handle = win32gui.GetForegroundWindow()
        if windowText in win32gui.GetWindowText(handle):
            break
        else:
            # increment counter and loop again                
            secondsPassed += 1

Then I used the SendKeys package to send text to the window (see http://www.rutherfurd.net/python/sendkeys/). If the user opens anything else, the script will break or weird things will happen. If you open something like MS Office, use win32com instead of SendKeys. That's much more reliable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号