【问题标题】:C# detecting key event in all windowsC#在所有窗口中检测关键事件
【发布时间】:2011-03-27 15:59:42
【问题描述】:

嘿,我对键事件处理程序有一些问题。这是来源:

using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Drawing;
        using System.Linq;
        using System.Text;
        using System.Windows.Forms;
        using System.Diagnostics;
        using System.Threading;

        namespace WindowsFormsApplication3
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }

                [System.Runtime.InteropServices.DllImport("user32.dll")]
                public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);


        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        public const int MOUSEEVENTF_RIGHTUP = 0x10;


                public void Form1_KeyPessed(object sender, KeyEventArgs e)
                {
                    if (e.KeyCode == Keys.F2)
                    {
                        DrawSquare();
                    }
                }

                public void DrawSquare()
                {
                    int line = Convert.ToInt16(textBox1.Text);
                    int time = Convert.ToInt16(textBox2.Text);

                    int x = MousePosition.X;
                    int y = MousePosition.Y;
                    Cursor.Position = new Point(x - line / 2, y - line / 2);
                    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y + line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y + line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x + line / 2, y - line / 2);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x - line / 2, y - line / 2);
                    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                    Thread.Sleep(time);
                    Cursor.Position = new Point(x, y);
                }
            }      
        }

现在,当我在表单中按 F2 时,它只会绘制正方形,但我希望它适用于所有窗口。 我还需要什么? (这有点像完美形状的自动抽屉)

【问题讨论】:

  • documentation for mouse_event 上的有趣注释。它说:此功能已被取代。请改用SendInput
  • 为什么要删除我添加的标签?更多标签有助于对您的问题进行分类,以便其他人可以找到它。它还可以帮助回答问题的人找到它。他们经常遵循他们熟悉的特定标签。

标签: c# .net windows focus keyevent


【解决方案1】:

如果您只想处理几个组合键,可以使用RegisterHotKey。如果您想检测所有不同的关键事件,请使用the global hook as Paul suggested

【讨论】:

  • 我以前没有遇到过RegisterHotKey,但对于这个用例来说,这似乎是一个更简单的解决方案......
  • @Paul:是的,确实如此。您的全局挂钩解决方案非常出色,而且我一直在使用。但是对于一个简单的热键,这有点像用火箭筒杀死一只苍蝇。它会起作用,但它肯定是矫枉过正,实际上会损害整体性能。全局挂钩should really be used sparingly。当然,Windows API 太大了,以至于我在自己回答问题时经常忘记RegisterHotKey... :-)
  • 你能举个简单的例子吗?
【解决方案2】:

你需要一个全局钩子。这里有一个很棒的实现http://www.codeproject.com/KB/cs/globalhook.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2012-10-23
    • 2012-06-30
    • 2011-03-12
    相关资源
    最近更新 更多