如何创建在后台运行并对键盘输入做出反应的Python脚本?

问题描述 投票:1回答:2

目前我正在使用AutoHotKey通过键盘快捷键触发脚本。我喜欢用Python编程,而不是处理AutoHotKey,每次触摸我的AutoHotKey脚本时,我都希望能编写干净的AutoHotkey代码。

让我们使用简单的AutoHotKey脚本,在我按下插入键的任何窗口中打印hello world

foo(){
    send, "hello world"
}
Insert:: foo()

我如何在Windows上的Python3中做同样的事情?

python windows python-3.x background-process
2个回答
1
投票

你将不得不钩住窗户的g来实现这一目标。您可能必须通过ctypesCFFI模块执行此操作,因为pywin32中似乎不存在必要的API。

根据this page,您需要使用三个Windows API调用:


0
投票

您可以将StackOverflow的两个答案组合起来(几乎)解决此问题。

  1. 使用this答案(通过tehvan)创建一个类似getch()的方法来读取用户的一个字符,而不需要\n。 (从答案下面重复)
  2. 使用Python3版本的this答案(由Barafu Albino)在单独的进程中调用先前定义的_Getch()类。

请注意,以下代码仅适用于Python3,并使用任何键来停止进程,而不仅仅是插入键。

# This code is a combination of two StackOverflow answers
# (links given in the answer)

# ------- Answer 1 by tehvan -----------------------------------
class _Getch:
    """Gets a single character from standard input.  
       Does not echo to the screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()

class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch

class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()

getch = _Getch()

# -------- Answer 2 by Barafu Albino (modified) -------
import _thread

def input_thread(a_list):
    _Getch().__call__()
    a_list.append(True)

def do_stuff():
    a_list = []
    _thread.start_new_thread(input_thread, (a_list,))
    print('Press any key to stop.')
    while not a_list:
        pass  
        # This is where you can put the stuff 
        # you want to do until the key is pressed
    print('Stopped.')

do_stuff()
© www.soinside.com 2019 - 2024. All rights reserved.