Ejemplo 1: Pulse teclas en Python
import pyautogui
# Holds down the alt key
pyautogui.keyDown("alt")# Presses the tab key once
pyautogui.press("tab")# Lets go of the alt key
pyautogui.keyUp("alt")
Ejemplo 2: tipo Python en el teclado
pip install keyboard
import keyboard
keyboard.press_and_release('shift+s, space')
keyboard.write('The quick brown fox jumps over the lazy dog.')
keyboard.add_hotkey('ctrl+shift+a',print, args=('triggered','hotkey'))# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down',lambda: keyboard.write('foobar'))# Blocks until you press esc.
keyboard.wait('esc')# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@','[email protected]')# Block forever, like `while True`.
keyboard.wait()
Ejemplo 3: python de pulsación de tecla real
# Simulates actual physical action of pressing a keyboard key as opposed to programmatically# simulating a keypress. TIP: For cleaner code, save as separate file and import it.import ctypes
from ctypes import wintypes
import time
user32 = ctypes.WinDLL('user32', use_last_error=True)
INPUT_MOUSE =0
INPUT_KEYBOARD =1
INPUT_HARDWARE =2
KEYEVENTF_EXTENDEDKEY =0x0001
KEYEVENTF_KEYUP =0x0002
KEYEVENTF_UNICODE =0x0004
KEYEVENTF_SCANCODE =0x0008
MAPVK_VK_TO_VSC =0# msdn.microsoft.com/en-us/library/dd375731
VK_TAB =0x09
VK_MENU =0x12# C struct definitions
wintypes.ULONG_PTR = wintypes.WPARAM
classMOUSEINPUT(ctypes.Structure):
_fields_ =(("dx", wintypes.LONG),("dy", wintypes.LONG),("mouseData", wintypes.DWORD),("dwFlags", wintypes.DWORD),("time", wintypes.DWORD),("dwExtraInfo", wintypes.ULONG_PTR))classKEYBDINPUT(ctypes.Structure):
_fields_ =(("wVk", wintypes.WORD),("wScan", wintypes.WORD),("dwFlags", wintypes.DWORD),("time", wintypes.DWORD),("dwExtraInfo", wintypes.ULONG_PTR))def__init__(self,*args,**kwds):super(KEYBDINPUT, self).__init__(*args,**kwds)# some programs use the scan code even if KEYEVENTF_SCANCODE# isn't set in dwFflags, so attempt to map the correct code.ifnot self.dwFlags & KEYEVENTF_UNICODE:
self.wScan = user32.MapVirtualKeyExW(self.wVk,
MAPVK_VK_TO_VSC,0)classHARDWAREINPUT(ctypes.Structure):
_fields_ =(("uMsg", wintypes.DWORD),("wParamL", wintypes.WORD),("wParamH", wintypes.WORD))classINPUT(ctypes.Structure):class_INPUT(ctypes.Union):
_fields_ =(("ki", KEYBDINPUT),("mi", MOUSEINPUT),("hi", HARDWAREINPUT))
_anonymous_ =("_input",)
_fields_ =(("type", wintypes.DWORD),("_input", _INPUT))
LPINPUT = ctypes.POINTER(INPUT)def_check_count(result, func, args):if result ==0:raise ctypes.WinError(ctypes.get_last_error())return args
user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes =(wintypes.UINT,# nInputs
LPINPUT,# pInputs
ctypes.c_int)# cbSize# FunctionsdefPressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))defReleaseKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode,
dwFlags=KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))defAltTab():"""Press Alt+Tab and hold Alt key for 2 seconds
in order to see the overlay.
"""
PressKey(VK_MENU)# Alt
PressKey(VK_TAB)# Tab
ReleaseKey(VK_TAB)# Tab~
time.sleep(2)
ReleaseKey(VK_MENU)# Alt~if __name__ =="__main__":
AltTab()
Ejemplo 4: pulsación del teclado de Python
import keyboard # using module keyboardwhileTrue:# making a looptry:# used try so that if user pressed other than the given key error will not be shownif keyboard.is_pressed('q'):# if key 'q' is pressed print('You Pressed A Key!')break# finishing the loopexcept:break# if user pressed a key other than the given key the loop will break
Te invitamos a confirmar nuestro estudio exponiendo un comentario o puntuándolo te damos las gracias.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)