keybd_event模拟键盘按键,mouse_event怎么用

从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI:

* Moving the mouse and clicking in the windows of other applications.
* Sending keystrokes to applications (for example, to fill out forms).
* Take screenshots, and given an image (for example, of a button or checkbox), and find it on the screen.
* Locate an application's window, and move, resize, maximize, minimize, or close it (Windows-only, currently).
* Display alert and message boxes.

Much of this code is based on information gathered from Paul Barton's PyKeyboard in PyUserInput from 2013, itself derived from Akkana Peck's pykey in 2008 ( http://www.shallowsky.com/software/crikey/pykey-0.1 ), itself derived from her "Crikey" lib. 久经考验喽,Windows, Linux, OS X都支持唷。

看它的源码可知道keybd_event的用法,例如:

def _keyDown(key):
    """Performs a keyboard key press without the release. This will put that
    key in a held down state.

    NOTE: For some reason, this does not seem to cause key repeats like would
    happen if a keyboard key was held down on a text field.

    Args:
      key (str): The key to be pressed down. The valid names are listed in
      pyautogui.KEY_NAMES.

    Returns:
      None
    """
    if key not in keyboardMapping or keyboardMapping[key] is None:
        return

    needsShift = pyautogui.isShiftCharacter(key)

    """
    # OLD CODE: The new code relies on having all keys be loaded in keyboardMapping from the start.
    if key in keyboardMapping.keys():
        vkCode = keyboardMapping[key]
    elif len(key) == 1:
        # note: I could use this case to update keyboardMapping to cache the VkKeyScan results, but I've decided not to just to make any possible bugs easier to reproduce.
        vkCode = ctypes.windll.user32.VkKeyScanW(ctypes.wintypes.WCHAR(key))
        if vkCode == -1:
            raise ValueError('There is no VK code for key "%s"' % (key))
        if vkCode > 0x100: # the vk code will be > 0x100 if it needs shift
            vkCode -= 0x100
            needsShift = True
    """
    mods, vkCode = divmod(keyboardMapping[key], 0x100)

    for apply_mod, vk_mod in [(mods & 4, 0x12), (mods & 2, 0x11),
        (mods & 1 or needsShift, 0x10)]: #HANKAKU not supported! mods & 8
        if apply_mod:
            ctypes.windll.user32.keybd_event(vk_mod, 0, KEYEVENTF_KEYDOWN, 0) #
    ctypes.windll.user32.keybd_event(vkCode, 0, KEYEVENTF_KEYDOWN, 0)
    for apply_mod, vk_mod in [(mods & 1 or needsShift, 0x10), (mods & 2, 0x11),
        (mods & 4, 0x12)]: #HANKAKU not supported! mods & 8
        if apply_mod:
            ctypes.windll.user32.keybd_event(vk_mod, 0, KEYEVENTF_KEYUP, 0) #

虽然它是python写的,但对VC和C#程序员很有参考价值。我是做了个半拉子不大好使。

我从github下载了源码放在博客园里了:https://files.cnblogs.com/files/blogs/714801/pyautogui-master.zip

pyautogui\_pyautogui_win.py

ctypes -  A foreign function library for Python

>>> from ctypes import *
>>> print(windll.kernel32)
<WinDLL 'kernel32', handle ... at ...>

pyautogui的作者们:

Abhijeet Singh https://github.com/cseas
Al Sweigart https://github.com/asweigart/
Alexandr Orlov https://github.com/hey-sancho
alphaCTzo7G https://github.com/alphaCTzo7G
Andrew Selzer https://github.com/afs2015
Andy Dam https://github.com/andydam
Anwar A. Ruff https://github.com/aaruff
Ari Lacenski https://github.com/tensory
Ashok Fernandez https://github.com/ashokfernandez/
Brian Redmond https://github.com/bredmond
Christopher Valles https://github.com/christophervalles
clach04 https://github.com/clach04
Clayton A. Alves https://github.com/claytonaalves
cryzed https://github.com/cryzed
Daniel D. Beck https://github.com/ddbeck
danielboone https://github.com/danielboone
Davee Nguyen https://github.com/daveenguyen
David Siah https://github.com/dsiah
Denilson Figueiredo de Sá https://github.com/denilsonsa
Dominik Schmelz https://github.com/DIDoS
dragon778 https://github.com/dragon788
Duxxi https://github.com/sjhhh3
Eric https://github.com/bleuetnoir
ErtugrulSener https://github.com/ErtugrulSener
Fornost461 https://github.com/Fornost461
Harrison https://github.com/Sentdex
Hugo Salvador https://github.com/hugoesb
i-need-to-tell-you-something https://github.com/i-need-to-tell-you-something
jakibaki https://github.com/
Jeff Triplett https://github.com/jefftriplett
Jeremy R. Gray https://github.com/jeremygray
Jeromie Kirchoff https://github.com/JayRizzo
Joel Gomes da Silva https://github.com/joelgomes1994
johnborgmann https://github.com/johnborgmann
Jon Winsley https://github.com/glitchassassin
jorg-j https://github.com/jorg-j
Jose Riha https://github.com/jose1711
Julien Schueller https://github.com/jschueller
Korons https://github.com/Korons
lb1a https://github.com/lb1a
Lesmana Zimmer https://github.com/lesmana
liberme https://github.com/liberme
Matt Olsen https://github.com/digwanderlust
mvbentes https://github.com/mvbentes
nexcvon https://github.com/nexcvon
Oleg Höfling https://github.com/hoefling
optroot https://github.com/optroot
pgkos https://github.com/pgkos
qiujieqiong https://github.com/qiujieqiong
Ricardo Amendoeira https://github.com/ric2b
Scott Noyes https://github.com/snoyes
Sergio Encarnación https://github.com/Sergioenc28
sneakypete81 https://github.com/sneakypete81
Stefan Hoelzl https://github.com/stefanhoelzl
Stephen Ellis https://github.com/saellis
Steven Shave https://github.com/stevenshave
Tim Gates https://github.com/timgates42
Timothy Crory https://github.com/tcrory
undefx https://github.com/undefx
Vélmer Oliveira https://github.com/velmer
Yoshiaki Ono https://github.com/fx-kirin
Stefan Hoelzl https://github.com/stefanhoelzl

posted @ 2021-12-05 15:38  Fun_with_Words  阅读(321)  评论(0编辑  收藏  举报









 张牌。