导航:首页 > 电脑设备 > 电脑键盘vb编程

电脑键盘vb编程

发布时间:2022-11-17 12:32:13

⑴ 用vb编写程序,键盘控制鼠标点击

用键盘事件接收键盘输入,然后用API SetCursorPos 来移动鼠标到指定坐标 mouse_event 来模拟鼠标单击 声明部分Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

核心代码SetCursorPos j, i(j和i 自己写你想要的坐标)
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '鼠标单击一次

⑵ VB编写按键程序.

sendkeys

vbs SendKey用法

简单说,这个命令就是模拟键盘操作,将一个或多个按键指令发送到指定Windows窗口来控制应用程序运行,其使用格式为:
object.SendKeys string
"object":表示WshShell对象
"string":表示要发送的按键指令字符串,需要放在英文双引号中.

1.基本键

一般来说,要发送的按键指令都可以直接用该按键字符本身来表示,例如要发送字母"x",使用
WshShell.SendKeys "x" 即可.

也可直接发送多个按键指令,只需要将按键字符按顺序排列在一起即可.例如,要发送按键"cfan",可以使用" WshShell.SendKeys "cfan" ".

2.特殊功能键
对于需要与Shift、Ctrl、Alt三个控制键组合的按键SendKeys使用特殊字符来表示:
特殊控制键 特殊字符
Shift +
Ctrl ^
Alt %

如要发送的组合按键是同时按下Ctrl+E,需要用" WshShell.SendKeys "^e" "表示,
如果要发送的组合按键是按住Ctrl键的同时按下E与C两个键,这时应使用小括号把字母括起来,书写格式为" WshShell.SendKeys "^(ec)" ".这里要注意它与" WshShell.SendKeys "^ec" "的区别,后者表示组合按键是同时按住Ctrl和E键,然后松开Ctrl键,单独按下"C"字母键

由于"+"、"^"这些字符用来表示特殊的控制按键了,如何表示这些按键呢?只要用大括号括住这些字符即可.例如,要发送加号"+",可使用" WshShell.SendKeys "{+}" ".另外对于一些不会生成字符的控制功能按键,也同样需要使用大括号括起来按键号名称,例如要发送回车键,需要用" WshShell.SendKeys "{ENTER}" "表示,发送向下的方向键用" WshShell.SendKeys "{DOWN}" "表示.

小提示
如果需要发送多个重复的单字母按键,不必重复输入该字母,SendKdys允许使用简化格式进行描述,使用格式为"{按键 数字}".例如要发送10个字母"x",则输入" WshShell.SendKeys "{x 10}" "即可.

3.翻译上手实例
" WshShell.SendKeys "^{ESC}u" "这句翻译为:按下Ctrl+Esc组合键(相当于按Win键)打开"开始"菜单,按着按U键打开"关机"菜单.

---Just Do It---------------------------------
让VBS脚本自动在词本中输入一行文字"Hello, welcome to cfan".

Dim WshShell
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad"
WScript.Sleep 200
WshShell.AppActivate " 无标题 - 记事本 "
WshShell.SendKeys "hello, welcome to cfan"

我们最常用的记事本没有Word、WPS那样的自动定时存盘功能,其实利用VBS脚本再加上SendKeys命令,就能弥补这个遗憾.打开记事本,输入以下内容(以容易描述和分析,把代码分为四个部分):

'第一部分:定义变量和对象
Dim WshShell, AutoSaveTime, TXTFileName
AutoSaveTime=300000
Set WshShell=WScript.CreateObject("WScript.Shell")
TXTFileName=InputBox("请输入你要创建的文件名(不能用中文和纯数字):")

'第二部分:打开并激活记事本
WshShell.Run "notepad"
WScript.Sleep 200
WshShell.AppActivate "无标题 - 记事本"

'第三部分:用输入的文件名存盘
WshShell.SendKeys "^s"
WScript.Sleep 300
WshShell.SendKeys TXTFileName
WScript.Sleep 300
WshShell.SendKeys "%s"
WScript.Sleep AutoSaveTime

'第四部分:自动定时存盘
While WshShell.AppActivate (TXTFileName)=True
WshShell.SendKeys "^s"
WScript.Sleep AutoSaveTime
Wend
WScript.Quit

将其保存为记事本.vbs,以后要使用记事本时,都通过双击这个脚本文件来打开.

程序翻译

这个脚本的基本思路是定时向记事本发送Ctrl+S这个存盘组合键.
第一部分:定义了脚本中需要用到的变量和对象."AutoSaveTime"变量用来设置自动存盘间隔,单位为毫秒,这里设置为5分钟."TXTFileName"变量通过输入框取得你要创建的文本文件名称.
第二部分:运行记事本,对于Windows本身提供的程序,比如计算器等,可直接在"WshShell.Run"后输入程序名称,如"calc"对于非系统程序,则可输入完全路径,但要注意使用8.3格式输入,比如" "D:\Progra~1\Tencent\QQ.exe" "
第三部分:这里用SendKeys命令执行了这样的操作流程(请注意每个操作之间延时命令的使用):
在记事本中按Ctrl+S组合键→弹出保存文件的窗口→输入文件名→按Alt+S组合键进行保存(默认保存在"我的文档"目录).
第四部分:定时存盘的关键,通过"While......Wend"这个当条件为"真"时循环命令,实现自动存盘代码" WshShell.SendKeys "^s" "和定时代码" WScript.Sleep AutoSaveTime "的重复执行.因为不能让这个定时存盘循环一直执行,退出记事本后,必须自动退出脚本并结束循环,所以设计了一个循环判断条件" WshShell.AppActivate TXTFileName=True ",当记事本运行中时,可以激活记事本窗口,这个条件运行结果为"True",定时存盘循环一直执行,退出记事本后,脚本无法激活记事本窗口,就会路出循环,执行"Wend"后面的"WScript.Quit"退出脚本.

---Just Do It---------------------------------
有款软件叫"QQ多账号自动登录器",它有记住QQ账号和密码,从而帮你用指定QQ号码自动登录,特别适合有多个QQ账号的朋友.其实利用VBS脚本你完全可以DIY一个.请到http://www.newhua.com/cfan/200419/vbs.rar下载脚本实例.其中有一个双QQ号码自动登录的脚本,请对照其中的解释将其修改为适合自己的QQ自动登录脚本.
请制作一个脚本,在你复制了一段文字后,只要双击运行这个脚本,就会自动打开记事本,并自动将复制的文字粘贴进来.
-----------------------------------------------

VBS中Sendkey键盘对应的码表

Key Code
------------------------------
Shift +
Ctrl ^
Alt %
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}

⑶ VB编写一个键盘快捷键程序,实现的功能是: 按下字母键"q",相当于数值"74100"; 按下字母

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyQ Then
MsgBox "74100"
ElseIf KeyCode = vbKeyW Then
MsgBox "74200"
ElseIf KeyCode = vbKeyE Then
MsgBox "74300"
ElseIf KeyCode = vbKeyR Then
MsgBox "74400"
'...............................
End If
End Sub

⑷ VB中知道了键盘各个按键的代码,怎样编写,举个例子吧!谁来开头!要控件的顺便说下

1.检测是否按了“↑”键,例
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyUp Then msgbox "您按了↑键!"
End Sub
2.检测按组合键
If (KeyCode = vbKeyNumpad0) And (Shift = 2) Then '按Ctrl+小键盘0
If (KeyCode = vbKeyF1) And (Shift = 2) Then '按Ctrl+F1
3.模拟按组合键
SendKeys "%{f4}" 'Alt+F4(关闭窗口)
SendKeys "{y 5}" '连续按5个y
SendKeys "Welcome" '发送字串

⑸ 怎样用VB模拟键盘输入

ivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SETTEXT = &HC

Private Sub Command1_Click()
Dim hwnd As Long, Thwnd As Long
Shell "notepad.exe", 2 '打开一个记事本,将它最小化
hwnd = FindWindow("Notepad", "无标题 - 记事本") '得到记事本句柄
Thwnd = FindWindowEx(hwnd, 0, "Edit", vbNullString) '得到记事本句柄(就是我们写字的那里)
SendMessage Thwnd, WM_SETTEXT, 0, ByVal "好了,快给我分" '向记事本写入文字
End Sub

⑹ VB一个简单的键盘按键响应程序

因为vbShiftMask,vbCtrlMask,vbAltMask是按位排列的,即2^n(n=0,1,2),分别为1,2,4,像这种情况,程序处理的时候就要用到位运算符(And)来处理这种数据。这个程序可以简化,你看看下面的。组合键情况显示在窗体Caption里。
代码如下。
=====================
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim s As String
If (Shift And vbShiftMask) = vbShiftMask Then s = s & "SHIFT" & " "
If (Shift And vbCtrlMask) = vbCtrlMask Then s = s & "CTRL" & " "
If (Shift And vbAltMask) = vbAltMask Then s = s & "ALT" & " "
Caption = s
End Sub

⑺ 用VB编写键盘映射程序

窗体KeyPreview设置为True
假设上方向键要映射为“1”键,则在keydown事件中这样写:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 38 Then KeyCode = 0: SendKeys "1"
End Sub

⑻ vb编程数字小键盘,1234567890

键盘键与虚拟键码对照表

字母和数字键 数字小键盘的键 功能键 其它键
键 键码键 键码 键 键码 键键码
A65 0 96 F1 112 Backspace 8
B66 1 97 F2 113 Tab 9
C67 2 98 F3 114 Clear 12
D68 3 99 F4 115 Enter 13
E69 4 100F5 116 Shift 16
F70 5 101F6 117 Control 17
G71 6 102F7 118 Alt 18
H72 7 103F8 119 Caps Lock 20
I73 8 104F9 120 Esc 27
J74 9 105F10121 Spacebar32
K75 * 106F11122 Page Up 33
L76 + 107F12123 Page Down 34
M77 Enter 108-- --End 35
N78 - 109-- -- Home36
O79 . 110-- -- Left Arrow37
P80 / 111-- -- Up Arrow38
Q81 -- ---- -- Right Arrow 39
R82 -- ---- -- Down Arrow 40
S83 -- ---- -- Insert 45
T84 -- ---- -- Delete 46
U85 -- ---- -- Help 47
V86 -- ---- -- Num Lock 144
W87
X88
Y89
Z90
048
149
250
351
452
553
654
755
856
957

⑼ VB常用键盘代码一览表

Keycode 常数

可在代码中的任何地方用下列常数代替实际值:

常数 值 描述
vbKeyLButton 0x1 鼠标左键
vbKeyRButton 0x2 鼠标右键
vbKeyCancel 0x3 CANCEL 键
vbKeyMButton 0x4 鼠标中键
vbKeyBack 0x8 BACKSPACE 键
vbKeyTab 0x9 TAB 键
vbKeyClear 0xC CLEAR 键
vbKeyReturn 0xD ENTER 键
vbKeyShift 0x10 SHIFT 键
vbKeyControl 0x11 CTRL 键
vbKeyMenu 0x12 MENU 键
vbKeyPause 0x13 PAUSE 键
vbKeyCapital 0x14 CAPS LOCK 键
vbKeyEscape 0x1B ESC 键
vbKeySpace 0x20 SPACEBAR 键
vbKeyPageUp 0x21 PAGE UP 键
vbKeyPageDown 0x22 PAGE DOWN 键
vbKeyEnd 0x23 END 键
vbKeyHome 0x24 HOME 键
vbKeyLeft 0x25 LEFT ARROW 键
vbKeyUp 0x26 UP ARROW 键
vbKeyRight 0x27 RIGHT ARROW 键
vbKeyDown 0x28 DOWN ARROW 键
vbKeySelect 0x29 SELECT 键
vbKeyPrint 0x2A PRINT SCREEN 键
vbKeyExecute 0x2B EXECUTE 键
vbKeySnapshot 0x2C SNAPSHOT 键
vbKeyInsert 0x2D INSERT 键
vbKeyDelete 0x2E DELETE 键
vbKeyHelp 0x2F HELP 键
vbKeyNumlock 0x90 NUM LOCK 键

A 至 Z 键与 A – Z 字母的 ASCII 码相同:

常数 值 描述
vbKeyA 65 A 键
vbKeyB 66 B 键
vbKeyC 67 C 键
vbKeyD 68 D 键
vbKeyE 69 E 键
vbKeyF 70 F 键
vbKeyG 71 G 键
vbKeyH 72 H 键
vbKeyI 73 I 键
vbKeyJ 74 J 键
vbKeyK 75 K 键
vbKeyL 76 L 键
vbKeyM 77 M 键
vbKeyN 78 N 键
vbKeyO 79 O 键
vbKeyP 80 P 键
vbKeyQ 81 Q 键
vbKeyR 82 R 键
vbKeyS 83 S 键
vbKeyT 84 T 键
vbKeyU 85 U 键
vbKeyV 86 V 键
vbKeyW 87 W 键
vbKeyX 88 X 键
vbKeyY 89 Y 键
vbKeyZ 90 Z 键

0 至 9 键与数字 0 – 9 的 ASCII 码相同:

常数 值 描述
vbKey0 48 0 键
vbKey1 49 1 键
vbKey2 50 2 键
vbKey3 51 3 键
vbKey4 52 4 键
vbKey5 53 5 键
vbKey6 54 6 键
vbKey7 55 7 键
vbKey8 56 8 键
vbKey9 57 9 键

下列常数代表数字键盘上的键:

常数 值 描述
vbKeyNumpad0 0x60 0 键
vbKeyNumpad1 0x61 1 键
vbKeyNumpad2 0x62 2 键
vbKeyNumpad3 0x63 3 键
vbKeyNumpad4 0x64 4 键
vbKeyNumpad5 0x65 5 键
vbKeyNumpad6 0x66 6 键
vbKeyNumpad7 0x67 7 键
vbKeyNumpad8 0x68 8 键
vbKeyNumpad9 0x69 9 键
vbKeyMultiply 0x6A MULTIPLICATION SIGN (*) 键
vbKeyAdd 0x6B PLUS SIGN (+) 键
vbKeySeparator 0x6C ENTER 键
vbKeySubtract 0x6D MINUS SIGN (–) 键
vbKeyDecimal 0x6E DECIMAL POINT (.) 键
vbKeyDivide 0x6F DIVISION SIGN (/) 键

下列常数代表功能键:

常数 值 描述
vbKeyF1 0x70 F1 键
vbKeyF2 0x71 F2 键
vbKeyF3 0x72 F3 键
vbKeyF4 0x73 F4 键
vbKeyF5 0x74 F5 键
vbKeyF6 0x75 F6 键
vbKeyF7 0x76 F7 键
vbKeyF8 0x77 F8 键
vbKeyF9 0x78 F9 键
vbKeyF10 0x79 F10 键
vbKeyF11 0x7A F11 键
vbKeyF12 0x7B F12 键
vbKeyF13 0x7C F13 键
vbKeyF14 0x7D F14 键
vbKeyF15 0x7E F15 键
vbKeyF16 0x7F F16 键

⑽ VB制作软键盘

用API keybd_event来模拟按键过程,但是Ctrl+Alt+Del不能模拟,前面已经定义了各个按键的值,需要按什么键替换成对应的值就行了,由于是模拟键盘按键,所以按键发送的对象是当前激活的窗口。

Option Explicit

'
'Virtual Keys, Standard Set
'
Private Const VK_LBUTTON = &H1
Private Const VK_RBUTTON = &H2
Private Const VK_CANCEL = &H3
Private Const VK_MBUTTON = &H4 'NOT contiguous with L & RBUTTON

Private Const VK_BACK = &H8
Private Const VK_TAB = &H9

Private Const VK_CLEAR = &HC
Private Const VK_RETURN = &HD

Private Const VK_SHIFT = &H10
Private Const VK_CONTROL = &H11
Private Const VK_MENU = &H12
Private Const VK_PAUSE = &H13
Private Const VK_CAPITAL = &H14

Private Const VK_KANA = &H15
Private Const VK_HANGEUL = &H15 'old name - should be here for compatibility
Private Const VK_HANGUL = &H15
Private Const VK_JUNJA = &H17
Private Const VK_FINAL = &H18
Private Const VK_HANJA = &H19
Private Const VK_KANJI = &H19

Private Const VK_ESCAPE = &H1B

Private Const VK_CONVERT = &H1C
Private Const VK_NONCONVERT = &H1D
Private Const VK_ACCEPT = &H1E
Private Const VK_MODECHANGE = &H1F

Private Const VK_SPACE = &H20
Private Const VK_PRIOR = &H21
Private Const VK_NEXT = &H22
Private Const VK_END = &H23
Private Const VK_HOME = &H24
Private Const VK_LEFT = &H25
Private Const VK_UP = &H26
Private Const VK_RIGHT = &H27
Private Const VK_DOWN = &H28
Private Const VK_SELECT = &H29
Private Const VK_PRINT = &H2A
Private Const VK_EXECUTE = &H2B
Private Const VK_SNAPSHOT = &H2C
Private Const VK_INSERT = &H2D
Private Const VK_DELETE = &H2E
Private Const VK_HELP = &H2F

'VK_0 thru VK_9 are the same as ASCII '0' thru '9' (=&H30 - =&H39)
'VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (=&H41 - =&H5A)

Private Const VK_LWIN = &H5B
Private Const VK_RWIN = &H5C
Private Const VK_APPS = &H5D

Private Const VK_NUMPAD0 = &H60
Private Const VK_NUMPAD1 = &H61
Private Const VK_NUMPAD2 = &H62
Private Const VK_NUMPAD3 = &H63
Private Const VK_NUMPAD4 = &H64
Private Const VK_NUMPAD5 = &H65
Private Const VK_NUMPAD6 = &H66
Private Const VK_NUMPAD7 = &H67
Private Const VK_NUMPAD8 = &H68
Private Const VK_NUMPAD9 = &H69
Private Const VK_MULTIPLY = &H6A
Private Const VK_ADD = &H6B
Private Const VK_SEPARATOR = &H6C
Private Const VK_SUBTRACT = &H6D
Private Const VK_DECIMAL = &H6E
Private Const VK_DIVIDE = &H6F
Private Const VK_F1 = &H70
Private Const VK_F2 = &H71
Private Const VK_F3 = &H72
Private Const VK_F4 = &H73
Private Const VK_F5 = &H74
Private Const VK_F6 = &H75
Private Const VK_F7 = &H76
Private Const VK_F8 = &H77
Private Const VK_F9 = &H78
Private Const VK_F10 = &H79
Private Const VK_F11 = &H7A
Private Const VK_F12 = &H7B
Private Const VK_F13 = &H7C
Private Const VK_F14 = &H7D
Private Const VK_F15 = &H7E
Private Const VK_F16 = &H7F
Private Const VK_F17 = &H80
Private Const VK_F18 = &H81
Private Const VK_F19 = &H82
Private Const VK_F20 = &H83
Private Const VK_F21 = &H84
Private Const VK_F22 = &H85
Private Const VK_F23 = &H86
Private Const VK_F24 = &H87

Private Const VK_NUMLOCK = &H90
Private Const VK_SCROLL = &H91

'
'VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
'Used only as parameters to GetAsyncKeyState() and GetKeyState().
'No other API or message will distinguish left and right keys in this way.
'
Private Const VK_LSHIFT = &HA0
Private Const VK_RSHIFT = &HA1
Private Const VK_LCONTROL = &HA2
Private Const VK_RCONTROL = &HA3
Private Const VK_LMENU = &HA4
Private Const VK_RMENU = &HA5

'dwFlags
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Sub Command1_Click()
Call keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Alt键
Call keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Print Screen键
Call keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Print Screen键
Call keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Alt键

Call keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Win键
Call keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Win键

Call keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY, 0) '按下Num Lock键
Call keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0) '抬起Num Lock键
End Sub

阅读全文

与电脑键盘vb编程相关的资料

热点内容
电脑上怎么下载班智达的软件 浏览:1152
无痕迹消除图片软件 浏览:715
免费小票软件 浏览:949
华为在哪里设置软件停止运行 浏览:956
用电脑键盘调节声音大小 浏览:1255
自动刷软件赚钱 浏览:1257
古装连续剧免费版 浏览:1410
工免费漫画 浏览:1141
手机软件专门储存文件 浏览:1504
uos如何用命令安装软件 浏览:1312
有线耳机插电脑麦克风 浏览:642
侏罗纪世界3在线观看完整免费 浏览:991
单个软件怎么设置名称 浏览:716
凤凰网电脑版下载视频怎么下载视频怎么下载 浏览:1380
明白之后如何免费获得无人机 浏览:827
如何解禁软件菜单 浏览:847
副路由器连接电脑视频 浏览:1347
内置wifi电视如何装软件 浏览:1099
手机换零免费雪碧 浏览:1584
国行苹果如何下载美版软件 浏览:1204