用C#捕捉键盘和鼠标

2019-03-02 23:39|来源: 网路

用C#捕捉键盘和鼠标
直接把下面代码copy到VS05编译器中运行.
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
public class Form1 : Form
{
    private Label label1;
    private System.ComponentModel.Container components = null;

    public Form1()
    {
       //初始化窗体中的各个组件
        InitializeComponent();
    }
    protected override void Dispose(bool disposing)
    {
      //清除程序中使用过的资源
        if (disposing)
        {
            if (components != null)
            {
                components.

                    Dispose();
            }
        }
        base.Dispose(disposing);
    }
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        //
        // label1
        //
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(145, 71);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(119, 12);
        this.label1.TabIndex = 0;
        this.label1.Text = "按回车建.可直接退出";
        //
        // Form1
        //
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
        this.ClientSize = new System.Drawing.Size(565, 273);
        this.Controls.Add(this.label1);
        this.KeyPreview = true;
        this.Name = "Form1";
        this.Text = "C#捕捉键盘和鼠标 By感觉De味道";
        this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
        this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    //程序开始.
    static void Main()
    {
        Application.Run(new Form1());
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        MessageBox.Show(e.KeyCode.ToString(), "您所按动的健为:");

        switch (e.KeyCode)
        {
            case Keys.A: System.Diagnostics.Process.Start(@"D:\StormII\Storm.exe"); break;
             
            case Keys.Enter: this.Close(); break;//回车退出程序.

          //  default: MessageBox.Show("BEY"); break;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
       
    }

private void Form1_MouseDown ( object sender , MouseEventArgs e )
{

if ( e.Button == MouseButtons.Left )
{
MessageBox.Show ( "按动鼠标左键!" ) ;
}
if ( e.Button == MouseButtons.Middle )
{
MessageBox.Show ( "按动鼠标中键!") ;
}
if ( e.Button == MouseButtons.Right )
{
MessageBox.Show ( "按动鼠标右键!") ;
}
}
//获取当前鼠标的位置.
    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        this.Text = "当前鼠标的位置为:( " + e.X + " , " + e.Y + ") By感觉De味道";
    }

    private void button1_Click_1(object sender, EventArgs e)
    {

    }

}

 

 


转自:http://www.cnblogs.com/zhoufleru/archive/2007/06/05/772515

相关问答

更多
  • 不知道你反映的问题是 键盘坏了,还是鼠标坏了.... 在BIOS里键盘可以用,说明键盘正常... 进入系统,鼠标可以用,说明鼠标也正常... 那问题是什么呢? 只要一按键盘,电脑就死机,是吗? 那说明你系统有问题...不是键盘,鼠标故障...键盘,鼠标是硬件.这样坏了就用不了..很好判断... 电脑死机多半是病毒引起..杀杀毒..实在不行,就GHOST吧...3分钟就可以把系统还原....
  • Windows挂钩 ! Windows钩子背后的概念是您的程序将自身插入到操作系统的消息链中,并且可以读取/拦截这些消息。 您还可以使用Windows钩子发送输入。 Windows钩子api是一个C ++ API,所以你必须使用一些DLLImports来获取项目中的这些本机方法。 如下。 这些是您接收消息所需功能的导入。 您还需要导入SendInput函数。 //Imports for Windows Hooks operations [DllImport("user32.dll", Cha ...
  • 原始输入可能是您要查找的内容。 这里是一个MSDN讨论,其中有一个类似的问题,它链接到一个关于处理C#中的多重键盘的codeproject文章。 Raw input might be what you're looking for. Here is an MSDN discussion with a similar question which has link to a codeproject article on handling mutliple keyboards in C#.
  • 看看Cursor.Position属性 。 应该让你开始 private void MoveCursor() { // Set the Current cursor, move the cursor's Position, // and set its clipping rectangle to the form. this.Cursor = new Cursor(Cursor.Current.Handle); Cursor.Position = new Point(Cursor ...
  • 作为DirectSound的替代品,还有多媒体API。 我知道没有包含在.net中的绑定,但有几个不同质量的第三方绑定。 naudio是一个流行的.net音频库,但我不知道它在后台使用了哪个API。 只要你避免称为WaveBuffer的憎恶,你应该没问题。 在我的项目中,我使用了Lumisoft.Audio ,主要是因为它有一个简单的API。 但是他们的代码没有很高的质量。 As an alternative to DirectSound there are the multi-media APIs. I ...
  • AFAIK,无法捕获GLOBAL键盘和鼠标事件。 但CodeProject上有几篇文章演示了.NET类包装的创建。 你可以在这里查看它们: 1) 在C#中处理全局鼠标和键盘挂钩 2) 全球鼠标和键盘库 编辑:顺便说一句,我在C#中使用第一个库创建了一个小键盘记录:) AFAIK, there's no way to capture the GLOBAL keyboard & mouse events. But there are a few articles on CodeProject which dem ...
  • 这是我在VB.NET中使用的模块(我会尽可能地翻译它,但是有一个在线工具可以自动完成): Imports System.Runtime.InteropServices Public Module KeyboardHook Public Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KBDLLHookProc, ByVal hInstance As ...
  • 您需要加载并使用DLL的内容。 DLL本身不会运行。 我们需要更多关于如何使用这个库的信息。 您可以创建仅具有处理类/函数的库,但必须在某处监控键盘。 这是一篇讨论全局键盘监控的文章。 该问题的答案连接到MSDN文章 。 回到我最初提到的,你不需要在你的图书馆中有实际的事件。 您可以创建仅包含功能代码的库。 然后可以在处理事件的整个应用程序中从更高级调用该代码。 这是一个非常简单而且老实的黑客例子: public static class MyKeyboardActions { public voi ...
  • 创建一个SDL_event结构并填写http://wiki.libsdl.org/SDL_KeyboardEvent和http://wiki.libsdl.org/SDL_Keysym中记录的字段,然后使用SDL_Pushevent()将事件放入事件队列: http ://wiki.libsdl.org/SDL_PushEvent SDL_Event event; event.type = SDL_KEYDOWN; event.timestamp = lastEvent.timestamp + 1; ...
  • 我找不到任何已经制作的东西了。 但我找到了一个起点: xdotool是一个从命令行控制鼠标和键盘的项目。 它也是开源的,所以如果你愿意,你可以在C#中本地实现相同的功能(或者如果需要,可以使用一些P / Invokes)。 I've not been able to find anything already-made. But I found a starting point for you: xdotool is a project to control mouse and keyboard from ...