using system;
using system.runtime.interopservices;
using system.text;
namespace consoleapplication8{
class class1{
[stathread]
static void main(string[] args){
// display current status of keys.
console.writeline(
"**before**/r/ncap: {0}/r/nscr: {1}/r/nnum: {2}",
keyboard.getstate(virtualkeys.vk_capital)?"on":"off",
keyboard.getstate(virtualkeys.vk_scroll)?"on":"off",
keyboard.getstate(virtualkeys.vk_numlock)?"on":"off"
);
// toggle all the keys:
keyboard.setstate(
virtualkeys.vk_capital,
!keyboard.getstate(virtualkeys.vk_capital)
);
keyboard.setstate(
virtualkeys.vk_scroll,
!keyboard.getstate(virtualkeys.vk_scroll)
);
keyboard.setstate(
virtualkeys.vk_numlock,
!keyboard.getstate(virtualkeys.vk_numlock)
);
// display new status of keys.
console.writeline(
"/r/n**after**/r/ncap: {0}/r/nscr: {1}/r/nnum: {2}",
keyboard.getstate(virtualkeys.vk_capital)?"on":"off",
keyboard.getstate(virtualkeys.vk_scroll)?"on":"off",
keyboard.getstate(virtualkeys.vk_numlock)?"on":"off"
);
console.readline();
}
}
public enum virtualkeys: byte{
vk_numlock = 0×90,
vk_scroll = 0×91,
vk_capital = 0×14
}
class keyboard{
const uint keyeventf_extendedkey = 0×1;
const uint keyeventf_keyup = 0×2;
[dllimport("user32.dll")]
static extern short getkeystate(int nvirtkey);
[dllimport("user32.dll")]
static extern void keybd_event(
byte bvk,
byte bscan,
uint dwflags,
uint dwextrainfo
);
public static bool getstate(virtualkeys key){
return (getkeystate((int)key)==1);
}
public static void setstate(virtualkeys key, bool state){
if(state!=getstate(key)){
keybd_event(
(byte)key,
0×45,
keyeventf_extendedkey ¦ 0,
0
);
keybd_event(
(byte)key,
0×45,
keyeventf_extendedkey ¦ keyeventf_keyup,
0
);
}
}
}
}
———————————————————-
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace example107_模拟鼠标
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows form designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button1 = new system.windows.forms.button();
this.suspendlayout();
//
// button1
//
this.button1.location = new system.drawing.point(144, 176);
this.button1.name = "button1";
this.button1.tabindex = 0;
this.button1.text = "mouse";
this.button1.click += new system.eventhandler(this.button1_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(280, 237);
this.controls.addrange(new system.windows.forms.control[] {
this.button1});
this.name = "form1";
this.text = "form1";
this.doubleclick += new system.eventhandler(this.form1_doubleclick);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private system.windows.forms.button button1;
[system.runtime.interopservices.dllimport("user32")]
private static extern int mouse_event(int dwflags,int dx,int dy, int cbuttons, int dwextrainfo);
const int mouseeventf_move = 0×0001;
const int mouseeventf_leftdown = 0×0002;
const int mouseeventf_leftup = 0×0004;
const int mouseeventf_rightdown = 0×0008;
const int mouseeventf_rightup = 0×0010;
const int mouseeventf_middledown = 0×0020;
const int mouseeventf_middleup = 0×0040;
const int mouseeventf_absolute = 0×8000;
private void form1_doubleclick(object sender, system.eventargs e)
{
messagebox.show("double click");
}
private void button1_click(object sender, system.eventargs e)
{
mouse_event(mouseeventf_move,100,100,0,0);
//下面是模拟双击的
//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函数说明:
dwflags ——– long,下述标志的一个组合
mouseeventf_absolute
dx和dy指定鼠标坐标系统中的一个绝对位置。在鼠标坐标系统中,屏幕在水平和垂直方向上均匀分割成65535×65535个单元 - mouseeventf_move
移动鼠标
mouseeventf_leftdown
模拟鼠标左键按下
mouseeventf_leftup
模拟鼠标左键抬起
mouseeventf_rightdown
模拟鼠标右键按下
mouseeventf_rightup
模拟鼠标右键按下
mouseeventf_middledown
模拟鼠标中键按下
mouseeventf_middleup
模拟鼠标中键按下
dx
long,根据是否指定了mouseeventf_absolute标志,指定水平方向的绝对位置或相对运动
dy ————- long,根据是否指定了mouseeventf_absolute标志,指定垂直方向的绝对位置或相对运动
cbuttons ——- long,未使用
dwextrainfo —- long,通常未用的一个值。用getmessageextrainfo函数可取得这个值。可用的值取决于特定的驱动程序
函数功能:该函数综合鼠标击键和鼠标动作。
函数原型:void mouse_event(dword dwflags,dword dx,dword dwflags,oword dx,dword dy, dword dwdata, dword dwextralnfo);
参数:
dwflags:标志位集,指定点击按钮和鼠标动作的多种情况。此参数里的各位可以是下列值的任何合理组合:
moose_eventf_absolote:表明参数dx,dy含有规范化的绝对坐标。如果不设置此位,参数含有相对数据:相对于上次位置的改动位置。此标志可被设置,也可不设置,不管鼠标的类型或与系统相连的类似于鼠标的设备的类型如何。要得到关于相对鼠标动作的信息,参见下面备注部分。
mooseeventfmove:表明发生移动。
m00seeventf_leftdown:表明接按下鼠标左键。
m00seeventf_leftup:表明松开鼠标左键。
mooseeventf_rightdown:表明按下鼠标右键。
mooseeventf_rightup:表明松开鼠标右键。
mooseeventf_middledown:表明按下鼠标中键。
mooseeventf_middleup:表明松开鼠标中键。
mooseeventf_wheel:在windows nt中如果鼠标有一个轮,表明鼠标轮被移动。移动的数量由dwdata给出。
dx:指定鼠标沿x轴的绝对位置或者从上次鼠标事件产生以来移动的数量,依赖于mooseeventf_absolote的设置。给出的绝对数据作为鼠标的实际x坐标;给出的相对数据作为移动的mickeys数。一个mickey表示鼠标移动的数量,表明鼠标已经移动。
dy:指定鼠标沿y轴的绝对位置或者从上次鼠标事件产生以来移动的数量,依赖于mooseeventf_absolvte的设置。给出的绝对数据作为鼠标的实际y坐标,给出的相对数据作为移动的mickeys数。
dwdata:如果dwflags为mooseeventf_wheel,则dwdata指定鼠标轮移动的数量。正值表明鼠标轮向前转动,即远离用户的方向;负值表明鼠标轮向后转动,即朝向用户。一个轮击定义为wheel_delta,即120。
如果dwflagss不是mooseeventf_wheel,则dwdata应为零。
dwextralnfo:指定与鼠标事件相关的附加32位值。应用程序调用函数getmessgeextrajnfo来获得此附加信息。
返回值:无。
备注:如果鼠标被移动,用设置mouseeventf_move来表明,dx和dy保留移动的信息。给出的信息是绝对或相对整数值。
如果指定了mowseeventf_absolote值,则dx和dy含有标准化的绝对坐标,其值在0到65535之间。事件程序将此坐标映射到显示表面。坐标(0,0)映射到显示表面的左上角,(6553,65535)映射到右下角。
如果没指定mowseeventf_absolote,dx和dy表示相对于上次鼠标事件产生的位置(即上次报告的位置)的移动。正值表示鼠标向右(或下)移动;负值表示鼠标向左(或上)移动。
鼠标的相对移动服从鼠标速度和加速度等级的设置,一个最终用户用鼠标控制面板应用程序来设置这些值,应用程序用函数systemparameterslnfo来取得和设置这些值。
在应用加速时系统对指定相对鼠标移动提供了两个测试。如果指定的沿x轴y轴的距离比第一个鼠标阈值大,并且鼠标的加速等级非零,则操作系统将距离加倍。如果指定的沿x轴或y轴的距离比第二个鼠标阈值大,并且鼠标的加速等级为2,则操作系统将从第一个阈测试得来的距离加倍。这样就允许操作系统将指定鼠标沿 x轴或y轴的相对位移加到4倍。
一旦应用了加速,系统用期望的鼠标速度换算合成的值。鼠标速度的范围是从1(最慢)到20(最快),并代表基于鼠标移动的距离指示符移动的数量。缺省值是10,表示对鼠标的移动设有附加的修改。
函数mouse_event需要用的应用程序用来合成鼠标事件。也被应用程序用来取得鼠标位置和鼠标按键状态之外的鼠标信息。例如,如果输入板制造商想将基于画笔的信息传给自己的应用程序,可以写一个直接与输入板硬件通信的动态键接库(dll),获得附加的信息,并保存到一个队列中。dll然后调用 mouse_event,用标准按键和x/y位置数据,并在参数dwextralnfo设置排列的附加信息的指针或索引。当应用程序需要附加信息时,调用 dll(连同存贮在dwextralnfo中的指针或索引),则dll返回附加信息。
windows ce:windows ce不支持参数 dwflags取moose eventf wheel常数。
速查: windows nt: 3.1及以上版本; windows:95及以上版本;windows ce:不支持;头文件:winuser.h;输入库:user32.lib。
Related posts: