如何检测Windows中按下的任何键并使用C#在运行时更改键值? [英] How can I detect any key pressed in windows and change the key value at runtime using C#?

查看:92
本文介绍了如何检测Windows中按下的任何键并使用C#在运行时更改键值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows中的任何位置检测用户输入,并希望用我的自定义值替换它。现在我使用钩子来检测按键但是我无法更改键值:就像用户在记事本中键入Hello一样,它想要用嘿!那里......替换它。



我尝试了什么:



我正在使用挂钩检测按键但是我无法更改键值:如果用户在记事本中键入Hello,则要将其替换为嘿!那里......

I want to detect the user input any where in windows and want to replace it with my custom value. For now I am using hooks to detect the key press but I am not able to change the key value: as like if user typed "Hello " in notepad the it want to replace it with "Hey! there..."

What I have tried:

I am using hooks to detect the key press but I am not able to change the key value: as like if user typed "Hello " in notepad the it want to replace it with "Hey! there..."

推荐答案

无法完成,因为当你的钩子收到第一个字符'H'时,它不知道下面的按键是否是'ello'。无论如何它必须让它通过。一旦它知道在处理'o'时已输入'Hello',则前面的键已经通过。



没有合理的目的来执行这样的全局替换,因为您的挂钩应用程序不知道实际接收键盘输入的应用程序。当它导致热键被执行时,甚至可能会做一些不需要的事情。
It can't be done because when your hook receives the first character 'H' it does not know if the following key presses are 'ello'. So it has to let it pass anyway. Once it knows that 'Hello' has been entered when handling the 'o', the preceeding keys has been already passed.

There is also no reasonable purpose to perform such replacements globally because your hooking application does not know about the application that actually receives the keyboard input. It might be even doing something unwanted when it results in hotkeys being executed.


这是单向:



Here is the one way:

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

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Form.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)50:
                        e.KeyChar = (char)51;
                        break;
                    case (char)49:
                        MessageBox.Show("Form.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }
    }
}





按下2号,我将它改为3和 e.Handled 仍为假。


这篇关于如何检测Windows中按下的任何键并使用C#在运行时更改键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆