怎么模拟按下一个键?

Not 2004-03-31 11:47:21
RT
...全文
121 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aglie 2004-04-01
  • 打赏
  • 举报
回复
mark!
Bob 2004-04-01
  • 打赏
  • 举报
回复
以下引用 TheAres(班门斧) 的话 :)

对于模拟键盘,除了利用keybd_event,更简单的是使用sendkeys,而且keybd_event已经被sendinput取代。

具体代码参考:
请问,用C#如何实现模拟键盘输入
http://expert.csdn.net/Expert/topic/1055/1055110.xml?temp=.1404993

对于模拟鼠标,只好用SendInput,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/SendInput.asp

具体代码参考:
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&threadm=665201c200e8%24e3a1f550%2435ef2ecf%40TKMSFTNGXA11&rnum=3&prev=/groups%3Fq%3Dsendinput%2Bmouse%2Bc%2523%26hl%3Dzh-CN%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D665201c200e8%2524e3a1f550%252435ef2ecf%2540TKMSFTNGXA11%26rnum%3D3
Bob 2004-04-01
  • 打赏
  • 举报
回复
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 = 0x90,
VK_SCROLL = 0x91,
VK_CAPITAL = 0x14
}
class Keyboard{
const uint KEYEVENTF_EXTENDEDKEY = 0x1;
const uint KEYEVENTF_KEYUP = 0x2;
[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,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0
);
keybd_event(
(byte)Key,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0
);
}
}
}
}
//and you can do mouse_event operation like it.
wolftop 2004-04-01
  • 打赏
  • 举报
回复
给你个例子:
上面的代码是我作的一个用于键盘模拟鼠标的软件,实现了下面的功能:
1、启动后放在右下角的托盘中,实现实时的监控。(这个很容易,大家自已看看代码就行)。
2、注册全局热键四个,分别为上下左右四个键,实现在任意环境下点击四个键实现鼠标的微调,这里有几个问题和大家说一下:第一个就是如何注册热键,我用的是以前在天极网微软件开发者的一篇文章的代码,贴出来给大家看看:如何处理热键 :


protected override void WndProc( ref Message m )
{
const int WM_HOTKEY = 0x0312;

switch(m.Msg)
{
case WM_HOTKEY:

MessageBox.Show("Hotkey pressed");

ProcessHotkey();

break;
}
base.WndProc(ref m );
}
但是我发现一个问题,就是不能同时注册多个热键,如果想注册多个,不是不可以,是注册后不能分辩出哪个是左,哪个是右?想了很长时间,才找到一个办法,是这样的
注册热键时:
NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
NativeWIN32.RegisterHotKey(Handle, 100, modifiers, Keys.Right); //注册热键
NativeWIN32.RegisterHotKey(Handle, 101, modifiers, Keys.Left); //又注册了一个热键
NativeWIN32.RegisterHotKey(Handle, 102, modifiers, Keys.Up); //又注册了一个热键
NativeWIN32.RegisterHotKey(Handle, 103, modifiers, Keys.Down); //又注册了一个热键
taskbarNotifier2.CloseClickable=false;
taskbarNotifier2.TitleClickable=false;
taskbarNotifier2.ContentClickable=false;
taskbarNotifier2.EnableSelectionRectangle=false;
taskbarNotifier2.KeepVisibleOnMousOver=false;
taskbarNotifier2.ReShowonMouseOver=false;
taskbarNotifier2.Show(" 网际浪子简介","浪子是一个计算机的狂热爱好者,欢迎和我联系:qq10402852 email:huanghai@bdfsz.com.cn",500,6000,500);

}


处理消息时:


protected override void WndProc( ref Message m )
{

const int WM_HOTKEY = 0x0312;

switch(m.Msg)
{


case WM_HOTKEY:


if (m.WParam.ToInt32() == 100) Cursor.Position = new Point(Cursor.Position.X+1,Cursor.Position.Y);//右移一个 
if (m.WParam.ToInt32() == 101) Cursor.Position = new Point(Cursor.Position.X-1,Cursor.Position.Y);//左移一个 
if (m.WParam.ToInt32() == 102) Cursor.Position = new Point(Cursor.Position.X,Cursor.Position.Y-1);//上移一个 
if (m.WParam.ToInt32() == 103) Cursor.Position = new Point(Cursor.Position.X,Cursor.Position.Y+1);//下移一个 


break;


}
base.WndProc(ref m );
}

哈,一不小心把第三个密秘也告诉大家了,就是如何在没有焦点的情况下移动鼠标。具体的大家看代码吧,希望和你一起进步!


Powered by DvNews.net
Bob 2004-04-01
  • 打赏
  • 举报
回复
下面的代码示例说明如何使用 Send 方法。要运行该示例,请将以下代码粘贴到一个称为 Form1 的窗体中,该窗体包含称为 Button1 的按钮。应将该按钮控件的 TabIndex 属性设置为 0。当该示例运行时,双击该窗体以触发按钮的 Click 事件。确保事件与其事件处理方法相连接。
[Visual Basic]

' Clicking Button1 causes a message box to appear.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Click here!")
End Sub


' Use the SendKeys.Send method to trigger the Button1 click event
' and display the message box.
Private Sub Form1_DoubleClick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.DoubleClick

' Send the enter key; since the tab stop of Button1 is 0, this
' will trigger the click event.
SendKeys.Send("{ENTER}")
End Sub
[C#]

// Clicking Button1 causes a message box to appear.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
MessageBox.Show("Click here!");
}


// Use the SendKeys.Send method to trigger the Button1 click event
// and display the message box.
private void Form1_DoubleClick(object sender, System.EventArgs e)
{

// Send the enter key; since the tab stop of Button1 is 0, this
// will trigger the click event.
SendKeys.Send("{ENTER}");
}
xinshaw 2004-04-01
  • 打赏
  • 举报
回复
solares1 2004-04-01
  • 打赏
  • 举报
回复
学习ing
newman0708 2004-04-01
  • 打赏
  • 举报
回复
学习......

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧