0

I recently read an answer from this question where basicly everything gets resolved. The answer was this. I implemented it into my code base and everything seemed to work. But when using the KEY_EVENT it only works when other keys are not pressed. With that i mean when i press 'a' everything works fine, but when i use 'SHIFT+A' it doesnt work anymore.

I downloaded the demo project that he attached in the answer and this were the results i got.

The second result also applies to any other button combination. I can use CTRL or ALT and the result looks always exactly the same like the second picture. The only thing that changes is wVirtualKeyCode (Shift=16, Ctrl=17, Alt=18)

Does maybe someone have an answer why the key combinations wont work?

Here is the Start Method which does all the reading.

public static void Start()
    {
        if (!Run)
        {
            Run = true;
            IntPtr handleIn = GetStdHandle(STD_INPUT_HANDLE);
            new Thread(() =>
            {
                while (true)
                {
                    uint numRead = 0;
                    INPUT_RECORD[] record = new INPUT_RECORD[1];
                    record[0] = new INPUT_RECORD();
                    ReadConsoleInput(handleIn, record, 1, ref numRead);
                    if (Run)
                        switch (record[0].EventType)
                        {
                            case INPUT_RECORD.MOUSE_EVENT:
                                MouseEvent?.Invoke(record[0].MouseEvent);
                                break;
                            case INPUT_RECORD.KEY_EVENT:
                                KeyEvent?.Invoke(record[0].KeyEvent);
                                break;
                            case INPUT_RECORD.WINDOW_BUFFER_SIZE_EVENT:
                                WindowBufferSizeEvent?.Invoke(record[0].WindowBufferSizeEvent);
                                break;
                        }
                    else
                    {
                        uint numWritten = 0;
                        WriteConsoleInput(handleIn, record, 1, ref numWritten);
                        return;
                    }
                }
            }).Start();
        }
    }
Glenomat
  • 11
  • 2
  • Please show us some code. From your question, it's completely unclear what you do that "works" or "doesn't work". – PMF Aug 13 '22 at 16:27
  • Basicly the whole code is in the answer. This is what apparently gets all the Inputs from the Console. Most of the code is just the implementation of the structs for everything nd the thing that grabs everything is the Start Method thread that gets started. With working and not working i mean the following: In the pictures you see the states that the key event has, here the interesting part (At least from my knowledge) is the uChar and the wVirtualKeyCode. In the first picture the uChar is 'a' but the key Code is 65, which corresponds to the ASCII char 'A' and not 'a' – Glenomat Aug 13 '22 at 17:24
  • And when using the Shift button in combination with 'a' (Picture 2 example) you get the wVirtualKeyCode of 16 and the uChar is nothing. – Glenomat Aug 13 '22 at 17:25
  • Also i updated the original question to include the Start Method – Glenomat Aug 13 '22 at 17:25
  • Any reason you use so complicated Win32 calls instead of just `Console.ReadKey`? – PMF Aug 13 '22 at 17:28
  • Actually yes. I mostly want to use this beacuse it can give me the correct mouse position inside of the console. But that means it also grabs the keyboard input. My original Listener was only for the keyboard and it was just a thread with a readkey. But those in combination wont work. I dont know why but the keyboard input doesnt get recognized every time when using both – Glenomat Aug 13 '22 at 17:35

0 Answers0