C# 当按键被按下时发出通知

发布于 2024-12-28 12:24:37 字数 635 浏览 1 评论 0原文

您好,我正在编写一个程序,它会注意到何时按下特定的键并在该人正在写的地方写下特定的字母。 但我有一个问题,那就是我不想需要将该程序作为“制作的程序”。相反,我确实希望它在用户按下按键时触发并写出用户正在写入的特定字母或文本...

我希望您理解并感谢所有帮助,

这是我到目前为止得到的代码:

        static void Main(string[] args)
    {

        while (true)
        {
            ConsoleKeyInfo cki;
            cki = Console.ReadKey();
            Console.WriteLine(cki.Key.ToString());

            if (cki.Key.ToString() == "A" && (cki.Modifiers & ConsoleModifiers.Control) != 0)
            {
                System.Threading.Thread.Sleep(400);
                TSendKeys.SendWait("ø");
            }
        }
    }

Hello I am doing a program that will notice when a specific key is pressed and write an specific letter where the person is writing.
But i have a problem, it is that i not want to be needing to have the program as the "maked one". instead I do want it to trigger and write out a spesifick letter or text where the user is writing as the user is pressing the key...

I hope you understand and thanks for all help

here's the code i get so far:

        static void Main(string[] args)
    {

        while (true)
        {
            ConsoleKeyInfo cki;
            cki = Console.ReadKey();
            Console.WriteLine(cki.Key.ToString());

            if (cki.Key.ToString() == "A" && (cki.Modifiers & ConsoleModifiers.Control) != 0)
            {
                System.Threading.Thread.Sleep(400);
                TSendKeys.SendWait("ø");
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆离笙 2025-01-04 12:24:37

但我有一个问题,那就是我不想需要
程序作为“制作的程序”。相反,我确实希望它触发并写入
用户以用户身份书写的特定字母或文本
正在按键...

读这篇文章我认为您正在谈论 Windows 挂钩
这意味着用户在另一个应用程序(例如 Word)中打印某些内容,但您可以捕获该键(即使您的应用程序中没有按下该键)。如果这是您真正想要的,这个 CodeProject 文章就是您所需要的。

But i have a problem, it is that i not want to be needing to have the
program as the "maked one". instead I do want it to trigger and write
out a spesifick letter or text where the user is writing as the user
is pressing the key...

Reading this I think you're talking about Windows Hooks.
This means that user prints something in another application, let's say Word, but you're able to catch that key (even if it wasn't pressed in your app). If this is what you actually want, this CodeProject article is what you need.

弄潮 2025-01-04 12:24:37

您可能需要使用钩子,本文 详细解释了如何执行此操作

You will probably have to use a hook, this article explains how to do this in detail

む无字情书 2025-01-04 12:24:37

一种方法是模拟 Console.ReadLine 的行为,而实际上分别读取和输出每个字符。当读取特殊序列时,例如CTRL+A,可以拦截该行为并插入字符。基本实现如下。

private static string EmulateReadLine()
{
    StringBuilder sb = new StringBuilder();
    int pos = 0;
    Action<int> SetPosition = (i) =>
        {
            i = Math.Max(0, Math.Min(sb.Length, i));
            pos = i;
            Console.CursorLeft = i;
        };
    Action<char, int> SetLastCharacter = (c, offset) =>
        {
            Console.CursorLeft = sb.Length + offset;
            Console.Write(c);
            Console.CursorLeft = pos;
        };
    ConsoleKeyInfo key;
    while ((key = Console.ReadKey(true)).Key != ConsoleKey.Enter)
    {
        int length = sb.Length;
        if (key.Key == ConsoleKey.LeftArrow)
        {
            SetPosition(pos - 1);
        }
        else if (key.Key == ConsoleKey.RightArrow)
        {
            SetPosition(pos + 1);
        }
        else if (key.Key == ConsoleKey.Backspace)
        {
            if (pos == 0) continue;
            sb.Remove(pos - 1, 1);
            SetPosition(pos - 1);
        }
        else if (key.Key == ConsoleKey.Delete)
        {
            if (pos == sb.Length) continue;
            sb.Remove(pos, 1);
        }
        else if (key.Key == ConsoleKey.Home)
            SetPosition(0);
        else if (key.Key == ConsoleKey.End)
            SetPosition(int.MaxValue);
        else if (key.Key == ConsoleKey.Escape)
        {
            SetPosition(0);
            sb.Clear();
        }
        // CUSTOM LOGIC FOR CTRL+A
        else if (key.Key == ConsoleKey.A &&
            (key.Modifiers & ConsoleModifiers.Control) != 0)
        {
            string stringtoinsert = "^";
            sb.Insert(pos, stringtoinsert);
            SetPosition(pos + stringtoinsert.Length);
        }
        else if (key.KeyChar != '\u0000') // keys that input text
        {
            sb.Insert(pos, key.KeyChar);
            SetPosition(pos + 1);
        }

        // replace entire line with value of current input string
        Console.CursorLeft = 0;
        Console.Write(sb.ToString());
        Console.Write(new string(' ', Math.Max(0, length - sb.Length)));
        Console.CursorLeft = pos;
    }

    Console.WriteLine();
    return sb.ToString();
}

驱动:

static void Main(string[] args)
{
    string input = EmulateReadLine();
    Console.WriteLine("Entered {0}", input);
}

输入行时,按 CTRL+A 将插入“^”字符。箭头键、Escape、Home 和 End 的功能应与 Console.ReadLine 的正常功能相同。 (为了简化代码,自定义插入逻辑是硬连线的。)

One approach is to emulate the behavior of Console.ReadLine, while in actuality reading and outputting each character separately. When reading special sequences, such as CTRL+A, the behavior can be intercepted and character inserted. A basic implementation is below.

private static string EmulateReadLine()
{
    StringBuilder sb = new StringBuilder();
    int pos = 0;
    Action<int> SetPosition = (i) =>
        {
            i = Math.Max(0, Math.Min(sb.Length, i));
            pos = i;
            Console.CursorLeft = i;
        };
    Action<char, int> SetLastCharacter = (c, offset) =>
        {
            Console.CursorLeft = sb.Length + offset;
            Console.Write(c);
            Console.CursorLeft = pos;
        };
    ConsoleKeyInfo key;
    while ((key = Console.ReadKey(true)).Key != ConsoleKey.Enter)
    {
        int length = sb.Length;
        if (key.Key == ConsoleKey.LeftArrow)
        {
            SetPosition(pos - 1);
        }
        else if (key.Key == ConsoleKey.RightArrow)
        {
            SetPosition(pos + 1);
        }
        else if (key.Key == ConsoleKey.Backspace)
        {
            if (pos == 0) continue;
            sb.Remove(pos - 1, 1);
            SetPosition(pos - 1);
        }
        else if (key.Key == ConsoleKey.Delete)
        {
            if (pos == sb.Length) continue;
            sb.Remove(pos, 1);
        }
        else if (key.Key == ConsoleKey.Home)
            SetPosition(0);
        else if (key.Key == ConsoleKey.End)
            SetPosition(int.MaxValue);
        else if (key.Key == ConsoleKey.Escape)
        {
            SetPosition(0);
            sb.Clear();
        }
        // CUSTOM LOGIC FOR CTRL+A
        else if (key.Key == ConsoleKey.A &&
            (key.Modifiers & ConsoleModifiers.Control) != 0)
        {
            string stringtoinsert = "^";
            sb.Insert(pos, stringtoinsert);
            SetPosition(pos + stringtoinsert.Length);
        }
        else if (key.KeyChar != '\u0000') // keys that input text
        {
            sb.Insert(pos, key.KeyChar);
            SetPosition(pos + 1);
        }

        // replace entire line with value of current input string
        Console.CursorLeft = 0;
        Console.Write(sb.ToString());
        Console.Write(new string(' ', Math.Max(0, length - sb.Length)));
        Console.CursorLeft = pos;
    }

    Console.WriteLine();
    return sb.ToString();
}

To drive:

static void Main(string[] args)
{
    string input = EmulateReadLine();
    Console.WriteLine("Entered {0}", input);
}

When entering the line, pressing CTRL+A will insert the "^" character. Arrow keys, Escape, Home, and End should function as they do normally for Console.ReadLine. (To simplify the code, the custom insertion logic is hardwired in.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文