简体   繁体   中英

C# Write text when key held down

I've got a C# console application with some pieces of text that represent buttons so for example it looks like this [ D ][ E ][ F ][ G ]

When the user presses the button I want the button to be highlighted which is no problem as what im currently doing is rewriting over the button with Console.BackgroundColor set.

What I want to do is that they key be constantly highlighted while the key is held down but as soon as the key is lifted again the highlighting to be removed, if possible i'd also like multiple keys to be pressed at the same time. This is what I can't figure out how to do?

Hope that makes sense :)

Any help?

Thanks

If you are willing to add a reference to Windows.Forms, call Application.Run() to run a message queue, and call external Windows DLLs, you can do it using this code: http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

That page will show you how to hook the low-level key-down keyboard event.

To also hook key-up keyboard events, you'll need to add a WM_KEYUP constant:

    private const int WM_KEYDOWN = 0x0100;
    private const int WM_KEYUP = 0x0101;

And then modify the HookCallback method:

    private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            Console.WriteLine("Down:" + (Keys)vkCode);
        }
        else if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            Console.WriteLine("Up:" + (Keys)vkCode);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

That will give you "Up" and "Down" messages for every key press. From there you should be able to incorporate it into your app.

Good luck!

Sorry but Console App only has Keyboard event (that too not actually an event unless you are in a loop and check for keypress) No KeyDown. keyPress or KeyUp events. It has no events of the GUI world.

I'm afraid AFAIK that a Console app can't detect multiple simultaneous key presses (that aren't modifier keys such as shift, or ctrl) so that isn't going to work.

With regard to highlighting a key as long as it is pressed you need to have your Console.ReadKey in a loop. Something like the following (you need to implement RemoveHighlight and HighlightKey methods yourself):

ConsoleKeyInfo currentKeyPressed;
ConsoleKeyInfo lastKeyPressed;
do
{
    currentKeyPressed = Console.ReadKey();
    if (lastKeyPressed.Key == currentKeyPressed.Key)
        continue;
    RemoveHighlight();
    HighlightKey(keyPressed.Key);
    lastKeyPressed = currentKeyPressed;         
} while ((keyPressed.Key & ConsoleKey.Escape) != ConsoleKey.Escape);

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