转自 http://apps.hi.baidu.com/share/detail/14468670

Option Explicit

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

Private Const VK_ADD As Long = &H6B '加号键
Private Const VK_APPS As Long = &H5D 'Applications 键(相当于鼠标右键)
Private Const VK_BACK As Long = &H8 'BackSpace 键
Private Const VK_CAPITAL As Long = &H14 'Caps Lock 键(大小写转换键)
Private Const VK_CANCEL As Long = &H3 'Ctrl + Break 过程
Private Const VK_CONTROL As Long = &H11
Private Const VK_DECIMAL As Long = &H6E '小数点号键
Private Const VK_DELETE As Long = &H2E
Private Const VK_DIVIDE As Long = &H6F '除号键
Private Const VK_DOWN As Long = &H28
Private Const VK_END As Long = &H23
Private Const VK_ESCAPE As Long = &H1B
Private Const VK_F1 As Long = &H70
Private Const VK_F10 As Long = &H79
Private Const VK_F11 As Long = &H7A
Private Const VK_F12 As Long = &H7B
Private Const VK_F2 As Long = &H71
Private Const VK_F3 As Long = &H72
Private Const VK_F4 As Long = &H73
Private Const VK_F5 As Long = &H74
Private Const VK_F6 As Long = &H75
Private Const VK_F7 As Long = &H76
Private Const VK_F8 As Long = &H77
Private Const VK_F9 As Long = &H78
Private Const VK_HOME As Long = &H24
Private Const VK_INSERT As Long = &H2D
Private Const VK_LCONTROL As Long = &HA2
Private Const VK_LEFT As Long = &H25
Private Const VK_LMENU As Long = &HA4 '左 Alt 键
Private Const VK_LSHIFT As Long = &HA0
Private Const VK_LWIN As Long = &H5B
Private Const VK_MENU As Long = &H12 'Alt 键
Private Const VK_MULTIPLY As Long = &H6A '乘号键
Private Const VK_NEXT As Long = &H22 '下翻页键
Private Const VK_NUMLOCK As Long = &H90
Private Const VK_NUMPAD0 As Long = &H60
Private Const VK_NUMPAD1 As Long = &H61
Private Const VK_NUMPAD2 As Long = &H62
Private Const VK_NUMPAD3 As Long = &H63
Private Const VK_NUMPAD4 As Long = &H64
Private Const VK_NUMPAD5 As Long = &H65
Private Const VK_NUMPAD6 As Long = &H66
Private Const VK_NUMPAD7 As Long = &H67
Private Const VK_NUMPAD8 As Long = &H68
Private Const VK_NUMPAD9 As Long = &H69
Private Const VK_PAUSE As Long = &H13
Private Const VK_PRINT As Long = &H2A
Private Const VK_PRIOR As Long = &H21 '上翻页键
Private Const VK_RCONTROL As Long = &HA3
Private Const VK_RETURN As Long = &HD '回车键
Private Const VK_RIGHT As Long = &H27
Private Const VK_RMENU As Long = &HA5 '右 Alt 键
Private Const VK_RSHIFT As Long = &HA1
Private Const VK_RWIN As Long = &H5C
Private Const VK_SCROLL As Long = &H91 'Scroll Lock 键
Private Const VK_SEPARATOR As Long = &H6C '小键盘上的回车键
Private Const VK_SHIFT As Long = &H10
Private Const VK_SLEEP As Long = &H5F '休眠键
Private Const VK_SNAPSHOT As Long = &H2C 'Print Screen 键
Private Const VK_SPACE As Long = &H20
Private Const VK_SUBTRACT As Long = &H6D '减号键
Private Const VK_TAB As Long = &H9
Private Const VK_UP As Long = &H26
Private Const VK_OEM_1 As Long = &HBA 'Windows 2000:对于 US 标准键盘,是“;:”键
Private Const VK_OEM_2 As Long = &HBF 'Windows 2000:对于 US 标准键盘,是“/?”键
Private Const VK_OEM_3 As Long = &HC0 'Windows 2000:对于 US 标准键盘,是“`~”键
Private Const VK_OEM_4 As Long = &HDB 'Windows 2000:对于 US 标准键盘,是“[{”键
Private Const VK_OEM_5 As Long = &HDC 'Windows 2000:对于 US 标准键盘,是“\|”键
Private Const VK_OEM_6 As Long = &HDD 'Windows 2000:对于 US 标准键盘,是“]}”键
Private Const VK_OEM_7 As Long = &HDE 'Windows 2000:对于 US 标准键盘,是“单/双引号”键
Private Const VK_OEM_COMMA As Long = &HBC 'Windows 2000:对于任何国家/地区,是“,”键
Private Const VK_OEM_MINUS As Long = &HBD 'Windows 2000:对于任何国家/地区,是“-”键
Private Const VK_OEM_PERIOD As Long = &HBE 'Windows 2000:对于任何国家/地区,是“.”键
Private Const VK_OEM_PLUS As Long = &HBB 'Windows 2000:对于任何国家/地区,是“+”键

Private Const KEYEVENTF_KEYUP = &H2

/**********************

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

/****************

1) 显示桌面:

很多软件有显示桌面的功能, 并且大家的方法都是遍历窗口, 然后让它们最小化, 其实 win系统给咱们了一个非常方便的WIN键(就是键盘上在CTRL键和ALT键之间的那个带win标志的按键), 利用它, 可以轻松的完成显示桌面的功能.

keybd_event(VK_LWIN, 0, 0 ,0);
keybd_event('M', 0, 0 ,0);
keybd_event('M', 0, KEYEVENTF_KEYUP ,0);
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP,0);

其他的操作也类似, 比如直接显示开始的运行,就把上面的'M'换成'R'即可。

直接 keybd_event(VK_LWIN, 0, 0 ,0);
keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP,0);

直接显示“开始”对话框了。

2) 实现快速的全选

很多的时候,比如listctrl实现全选,你可以用listctrl循环设置每一项的状态为选中,多罗索的事情呀。用快捷键试一试CTRL+A,其他的快捷键一样的用法,呵呵,你知道怎么办了吧?

keybd_event(VK_CONTROL, (BYTE)0, 0 ,0);
keybd_event('A',(BYTE)0, 0 ,0); //此处可以用 'A', (BYTE)65, 用'a'不起作用.
keybd_event('A', (BYTE)0, KEYEVENTF_KEYUP,0);
keybd_event(VK_CONTROL, (BYTE)0, KEYEVENTF_KEYUP,0);

3) 执行某些特殊的键,比如数字键,大小写,下面是数字键的例子

bool bState=true; //true为按下NumLock,false反之
BYTE keyState[256];

GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[VK_NUMLOCK] & 1)) ||
(!bState && (keyState[VK_NUMLOCK] & 1)) )
{
// Simulate a key press
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );

// Simulate a key release
keybd_event( VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}

4) 你想CTRL+ALT+DELETE三键一起按下,

keybd_event(VK_CONTROL, 0, 0 ,0);
keybd_event(VK_MENU,0, 0 ,0);
keybd_event(VK_DELETE,0, 0 ,0);

keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP ,0);
keybd_event(VK_MENU,0, KEYEVENTF_KEYUP ,0);
keybd_event(VK_DELETE,0, KEYEVENTF_KEYUP ,0);
呵呵,这样不会成功呀,因为这几个键直接是操作系统来截获执行的,而模拟键盘只能发向应用程序,所以这种方法不行的(想显示锁定对话框,用 LockWorkStation();)

5) Window2000/NT/XP已经不提倡用这个函数了,上面的方法只是为了让大家开阔一下思路,怎么替代呢,呵呵,看下面,所以上面的所有代码都可以用这个来完成

//2000下用这个代替 ,包含 "winable.h"
INPUT input[4];
memset(input, 0, sizeof(input));

input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;

input[0].ki.wVk = input[3].ki.wVk = VK_LWIN;
input[1].ki.wVk = input[2].ki.wVk = 'R';

//接下来释放它,这一点很重要。
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[0].ki.time = input[1].ki.time = input[2].ki.time = input[3].ki.time = GetTickCount();

SendInput(4, input, sizeof(INPUT));

感觉比那个有点罗索,呵呵。

====================

附WIN键的部分快捷键:

WIN键+D=快速的切到桌面,再次点击返回

WIN键+E=快速打开资源管理器

WIN键+R=“运行”。

WIN键+M=全部视窗最小化。

WIN键+Shift+M=取消全部视窗最小化。

WIN键+F1=Help。

WIN键+F=“寻找”。

WIN键+Ctrl+F=显示“查找电脑”。

WIN键+Tab=切换工作列的程式。

WIN键+Break=显示系统内容。

keybd_event 转载的更多相关文章

  1. C# 发送消息SendKeys、SendMessage、keybd_event的用法

    一.C#中SendKeys的用法 功能:将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样. 语法: SendKeys.Send(string keys); SendKeys.SendWa ...

  2. Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

    作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...

  3. RTP与RTCP协议介绍(转载)

    RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步.RTP中没有连接的概念,本身并不能为按序传输数据包提供可靠的保证,也不提供流量控制和拥塞控制,这些都由RTCP来负责完 ...

  4. 《Walking the callstack(转载)》

    本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with so ...

  5. [转载]MVVM模式原理分析及实践

    没有找到很好的MVVM模式介绍文章,简单找了一篇,分享一下.MVVM实现了UI\UE设计师(Expression Blend 4设计界面)和软件工程师的合理分工,在SilverLight.WPF.Wi ...

  6. [转载]:STM32为什么必须先配置时钟再配置GPIO

    转载来源 :http://blog.csdn.net/fushiqianxun/article/details/7926442 [原创]:我来添两句,就是很多同学(包括我)之前搞低端单片机,到了stm ...

  7. [转载]从MyEclipse到IntelliJ IDEA-让你摆脱鼠标,全键盘操作

    从MyEclipse转战到IntelliJ IDEA的经历 注转载址:http://blog.csdn.net/luoweifu/article/details/13985835 我一个朋友写了一篇“ ...

  8. TCP同步与异步,长连接与短连接【转载】

    原文地址:TCP同步与异步,长连接与短连接作者:1984346023 [转载说明:http://zjj1211.blog.51cto.com/1812544/373896   这是今天看到的一篇讲到T ...

  9. 在CentOS 7/6.5/6.4 中安装Java JDK 8(转载)

    转载在CentOS 7/6.5/6.4 中安装Java JDK 8 首先,在你的服务器上运行一下更新. yum update 然后,在您的系统上搜索,任何版本的已安装的JDK组件. rpm -qa | ...

随机推荐

  1. Sequence

    http://poj.org/problem?id=2442 用STL写的时间为:5657MS #include<cstdio> #include<algorithm> #in ...

  2. Android 4.4 上实现透明导航栏和状态栏 Translucent system bar

    Translucent system UI styling To get the most impact out of your content, you can now use new window ...

  3. Power Network (最大流增广路算法模板题)

    Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20754   Accepted: 10872 Description A p ...

  4. COJ 1002 WZJ的数据结构(二)(splay模板)

    我的LCC,LCT,Splay格式终于统一起来了... 另外..这个形式的Splay是标准的Splay(怎么鉴别呢?看Splay函数是否只传了一个变量node就行),刘汝佳小白书的Splay写的真是不 ...

  5. 关于if语句后面的花括号

    两种写法.之前我比较2.总喜欢写了if语句后 不带 花括号.总感觉这样节省空间. 最后偶然看到google推荐的 才 顿悟到 这样虽然可以 但可读性不太好. 参考:https://source.and ...

  6. vs 2013调试的时候重启的解决方案

    今天在用vs 2013 调试程序的时候,vs 总是莫名其妙的关闭,停止运行,泪蹦了..... 是什么原因呢? 以前的时候可是好好的啊,经过认真的思索,最近装过和vs 2013 相关的程序也只有 ref ...

  7. centos 64bit 安装与升级 chrome的方法

    vim /etc/yum.repos.d/CentOS-Base.repo 根据你的系统增加一个节点 [google64] name=Google - x86_64 baseurl=http://dl ...

  8. HBase技术介绍

    HBase简介 HBase - Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HB ...

  9. php 中的$argv与$argc

    例如 php test.php as a joke print_r($argv); echo $argc; print_r($argv); array_shift($argv); echo $argc ...

  10. Duplicate headers received from server

    This ones a little old but was high in the google ranking so I thought I would throw in the answer I ...