delphi中如何模拟最简单的键盘输入?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi中如何模拟最简单的键盘输入?相关的知识,希望对你有一定的参考价值。

有没最简单的方法,例如我要按退格和回车两个键,但要相隔三秒,重复~ 谢了。

举个例子,模拟字符“a”的输入: keybd_event(65,0,0,0);
----------------------------------------------------------------
模拟键盘可以用Keybd_event这个api函数,模拟鼠标按键用mouse_event函数.

Keybd_event函数能触发一个按键事件,也就是会产生一个WM_KEYDOWN或WM_KEYUP消息,一般用这两个消息来模拟一

次按键(按键的过程是:按下,然后弹起),但是没有直接用这个函数方便。
Keybd_event共有四个参数:
第一个为按键的虚拟键值,如回车键为 vk_return, tab键为vk_tab;
第二个参数为扫描码,一般不用设置,用0代替就行;
第三个参数为选项标志,如果为 keydown则置0即可,如果为keyup则设成“KEYEVENTF_KEYUP”;
第四个参数一般也是置0即可。

例如用以下代码即可实现模拟按下i键,其中的$49表示i键的虚拟键值:

keybd_event($49,0,0,0);

keybd_event($49,0,KEYEVENTF_KEYUP,0);

mouse_event最好配合setcursorpos(x,y)函数一起使用,先定位鼠标,再产生鼠标事件.
mouse_event有五个参数:
第一个为选项标志,为MOUSEEVENTF_LEFTDOWN时表示左键按下,为 MOUSEEVENTF_LEFTUP表示左键松开,向系统发送相应消息;
第二三个参数分别表示x,y相对位置,一般可设为0,0;
第四五个参数并不重要,一般也可设为0,0。

mouse_event的示例代码:

setcursorpos(20,132);

mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);

mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);

mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);

mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); ...

上面的代码表示鼠标的双击,若要表示单击,用两个mouse_event即可(一次放下,一次松开)。

注意
不管是模拟键盘还是鼠标事件,都要注意还原,即按完键要松开,一个keydown对应一个keyup;鼠标单击 完也要松开, 不然可能影响程序的功能。

例子1:模拟按下'A'键
keybd_event(65,0,0,0);
keybd_event(65,0,KEYEVENTF_KEYUP,0);

例子2:模拟按下'ALT+F4'键
keybd_event(18,0,0,0);
keybd_event(115,0,0,0);
keybd_event(115,0,KEYEVENTF_KEYUP,0);
keybd_event(18,0,KEYEVENTF_KEYUP,0);

附:常用模拟键的键值对照表。

键盘键与虚拟键码对照表

字母和数字键 数字小键盘的键 功能键 其它键
键 键码 键 键码 键 键码 键 键码
A 65 0 96 F1 112 Backspace 8
B 66 1 97 F2 113 Tab 9
C 67 2 98 F3 114 Clear 12
D 68 3 99 F4 115 Enter 13
E 69 4 100 F5 116 Shift 16
F 70 5 101 F6 117 Control 17
G 71 6 102 F7 118 Alt 18
H 72 7 103 F8 119 Caps Lock 20
I 73 8 104 F9 120 Esc 27
J 74 9 105 F10 121 Spacebar 32
K 75 * 106 F11 122 Page Up 33
L 76 + 107 F12 123 Page Down 34
M 77 Enter 108 -- -- End 35
N 78 - 109 -- -- Home 36
O 79 . 110 -- -- Left Arrow 37
P 80 / 111 -- -- Up Arrow 38
Q 81 -- -- -- -- Right Arrow 39
R 82 -- -- -- -- Down Arrow 40
S 83 -- -- -- -- Insert 45
T 84 -- -- -- -- Delete 46
U 85 -- -- -- -- Help 47
V 86 -- -- -- -- Num Lock 144
W 87
X 88
Y 89
Z 90
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
参考技术A 是啊,键盘事件最简单 参考技术B keybd_event

在 Python 上模拟键盘和鼠标的最简单方法是啥?

【中文标题】在 Python 上模拟键盘和鼠标的最简单方法是啥?【英文标题】:Which is the easiest way to simulate keyboard and mouse on Python?在 Python 上模拟键盘和鼠标的最简单方法是什么? 【发布时间】:2011-02-17 00:33:07 【问题描述】:

我需要做一些宏,我想知道最推荐的方法是什么。

所以,我需要写一些东西并用它点击一些地方,我需要模拟 TAB 键。

【问题讨论】:

您可能希望更具体地了解您正在测试的内容。网页?桌面应用程序? 网页、桌面应用程序,一切 =) Here 是一种在 OS X 中模拟键盘事件的方法。 【参考方案1】:

我在 Python 中进行自动化测试。我倾向于使用以下内容:

http://www.tizmoi.net/watsup/intro.html编辑:链接已失效,存档版本:https://web.archive.org/web/20100224025508/http://www.tizmoi.net/watsup/intro.html

http://www.mayukhbose.com/python/IEC/index.php

我并不总是(几乎从不)模拟按键和鼠标移动。我通常使用 COM 来设置 windows 对象的值并调用它们的 .click() 方法。

你可以用这个发送按键信号:

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys("^a") # CTRL+A may "select all" depending on which window's focused
shell.SendKeys("DELETE") # Delete selected text?  Depends on context. :P
shell.SendKeys("TAB") #Press tab... to change focus or whatever

这一切都在 Windows 中。如果您在另一个环境中,我不知道。

【讨论】:

温馨提示:要使用win32com,您首先必须安装Python for Windows extensions。 要激活您要发送密钥的程序,请使用shell.AppActivate("Notepad")(替换记事本) 这非常有效 - 感谢@dagur 关于窗口焦点的提示!我还需要鼠标事件模拟 - this anser 对我来说是最好的,并且与这个键盘解决方案配合得很好。【参考方案2】:

也许您正在寻找Sendkeys?

SendKeys 是一个 Python 模块,用于 可以发送一个或多个的 Windows 击键或击键组合 到活动窗口。

好像只有windows

你还有pywinauto(复制自我的SO answer)

pywinauto 是一套开源的 (LGPL) 模块,用于将 Python 用作 Windows NT 的 GUI 自动化“驱动程序” 基于操作系统 (NT/W2K/XP)。

网页上的例子

> from pywinauto import application
> app = application.Application.start("notepad.exe")
> app.notepad.TypeKeys("%FX")
> app.Notepad.MenuSelect("File->SaveAs")
> app.SaveAs.ComboBox5.Select("UTF-8")
> app.SaveAs.edit1.SetText("Example-utf8.txt")
> app.SaveAs.Save.Click()

【讨论】:

SendKeys 似乎仅适用于 Python 2.x。而且,它的网站现在已经死了【参考方案3】:

pyautogui 是一个很棒的包,可以发送键和自动化几个键盘/鼠标相关的任务。查看Controlling the Keyboard and Mouse with GUI Automation 和PyAutoGUI’s documentation。

【讨论】:

【参考方案4】:

您可以将PyAutoGUI library 用于适用于 Windows、macOS 和 Linux 的 Python。

鼠标

下面是一个简单的代码,将鼠标移动到屏幕中间:

import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

文档页面:Mouse Control Functions.

相关问题:Controlling mouse with Python。

键盘

例子:

pyautogui.typewrite('Hello world!')                 # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character

文档页面:Keyboard Control Functions.


更多阅读:Controlling the Keyboard and Mouse with GUI Automation(电子书第18章)。

相关问题:

Python GUI automation library for simulating user interaction in apps。 Python simulate keydown。

【讨论】:

【参考方案5】:

另外两个选项是:

pynput - https://pypi.org/project/pynput/ - 适用于 Windows(已测试)、Linux 和 MacOS - 文档位于 https://pynput.readthedocs.io/en/latest/ PyDirectInput - https://pypi.org/project/PyDirectInput/ - 仅适用于 Windows,可与(或不与)PyAutoGUI 一起使用

警告 - 如果您想在游戏中使用键盘控制,那么 pynput 并不总是有效 - 例如。它适用于 Valheim,但不适用于 Witcher 3 - PyDirectInput 将在其中工作。我还测试了 PyDirectInput,它适用于《半条命 2》(作为对旧游戏的测试)。

提示 - 您可能需要减少(不要在游戏中删除)字符输入之间的延迟 - 使用 pydirectinput.PAUSE = 0.05

例如,这里有一个允许虚拟键盘输入的功能 - 目前仅在 Windows 上测试:

from pynput import keyboard
try:
    import pydirectinput
    pydirectinput.PAUSE = 0.05
except ImportError as err:
    pydirectinput = False
    print("pydirectinput not found:")

def write_char(ch):
    upper = ch.isupper()
    if pydirectinput and pydirectinput.KEYBOARD_MAPPING.get(ch.lower(), False):
        if upper:
            pydirectinput.keyDown('shift')
            print('^')
        pydirectinput.write(ch.lower(), interval=0.0)
        print(ch)
        if upper:
            pydirectinput.keyUp('shift')
    else:
        keyboard.Controller().type(ch)

这允许发送一个字符串,通过 pydirectinput 处理大写字母字符。当字符不简单地映射时,该函数回退到使用 pynput。请注意,PyAutoGUI 也无法处理一些移位的字符 - 例如 £ 符号等。

【讨论】:

以上是关于delphi中如何模拟最简单的键盘输入?的主要内容,如果未能解决你的问题,请参考以下文章

delphi 键盘值

c语言 怎样模拟鼠标键盘操作?

用C#语言在浏览器中模拟键盘输入和鼠标点击

c语言(用最简单的,初学)从键盘输入四个整数,求平均值.数据输入,计算结果和输出要求有注释

在 C# 中模拟键盘输入

delphi键盘钩子没效果