简体   繁体   中英

C# Notice when a key is pressed

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("ø");
            }
        }
    }

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.

您可能必须使用一个钩子, 本文将详细介绍如何执行此操作。

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM