推荐文章

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增

C#模拟键盘操作(Keybd_event)

日期:2018-08-15 点击:1818 来源:PB2.CN

Windows提供了一个模拟键盘API函数Keybd_event(),使用该函数可以相应的屏蔽键盘的动作。Keybd_event()函数能触发一个按键事件,也就是说会产生一个WM_KEYDOWN或WM_KEYUP消息。


user32引用

[System.Runtime.InteropServices.DllImport("user32")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);


Windows API

void keybd_event(
  BYTE bVk,                   // 按键的虚拟键值,如回车键为vk_return, tab键为vk_tab
  BYTE bScan,               // 扫描码,一般不用设置,用0代替就行
  DWORD dwFlags,      // 选项标志,如果为keydown则置0即可,如果为keyup则设成"KEYEVENTF_KEYUP"
  DWORD dwExtraInfo // 一般也是置0即可
);



模拟按下'A'键
keybd_event(65,0,0,0);
keybd_event(65,0,KEYEVENTF_KEYUP,0);


模拟按下'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);


模拟按下'ctrl+v'键
keybd_event((byte)Keys.ControlKey, 0, 0, 0);//按下
keybd_event((byte)Keys.V, 0, 0, 0);
keybd_event((byte)Keys.ControlKey, 0, 0x2, 0);//松开
keybd_event((byte)Keys.V, 0, 0x2, 0);


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


按键虚拟键值
A-Z65-90
0-948-57
小键盘0-996-105
*106
+107
小键盘Enter108
-109
.110
/111
F1-F12112-123



这篇文档对您是否有帮助?

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符

c#去除字符串中的空格,回车,换行符,制表符
C#监控程序启动和关闭

C#监控程序启动和关闭

C#监控程序启动和关闭using System;using System.Collections.Generic;using System.Text;using System.Diagnostics;using System.Threading;namespace ProcessListener{ class Program { static void Main(stri
C#操作USB口的摄像头

C#操作USB口的摄像头

private const uint BM_CLICK = 0xF5; 鼠标点击的消息,对于各种消息的数值,查API手册,也可用VS2010自带的SPY++ [DllImpt("user32.dll", EntryPoint = "SendMessage", SetLastErr = true, Set = Set.Auto)]private static extern int SendMe
C# FTP操作

C# FTP操作

C# FTP操作
C#文件监控对象FileSystemWatcher

C#文件监控对象FileSystemWatcher

使用C#文件监控对象FileSystemWatcher对文件夹下的删除、修改、新增