0

I want to know how do I detect if the 'alt' key is being pressed like when typing the '@' symbol you press the alt key and 'à'and also I cannot use _KeyDown event because I'm making a class.

Ok so i have just tryed this:

if (Control.ModifierKeys.ToString() == "Alt")
{    
    MessageBox.Show("ALT");
}

but this only works with the left alt key for some reason..

urmat abdykerimov
  • 427
  • 1
  • 7
  • 17
  • what ui framework? wpf? maui? winforms? – Daniel A. White Jul 15 '21 at 01:11
  • there is no UI since like i said it a classe so pretty much a console app – cipher_449449 Jul 15 '21 at 01:15
  • possible duplicate: https://stackoverflow.com/questions/33490168/how-to-check-if-ctrl-key-is-pressed-in-console-application-c-sharp – Daniel A. White Jul 15 '21 at 01:16
  • No it no duplicate i have just tried something look at the post. – cipher_449449 Jul 15 '21 at 01:23
  • please give more context. You might have to invoke the win32 api to get a message loop going – Daniel A. White Jul 15 '21 at 01:23
  • well i want to capture if the key alt is being pressed so that if the value is true then convert the char à to @ does this make you understand what i want to do also what do you mean by invoking the win32 api to get... ? – cipher_449449 Jul 15 '21 at 01:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234879/discussion-between-cipher-449449-and-daniel-a-white). – cipher_449449 Jul 15 '21 at 01:34
  • 1
    [GetAsyncKeyState](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate) -- [GetKeyboardState](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeyboardstate) -- Note that ModifierKeys returns `Control ALT` when ALT Gr is pressed, just ALT when the left ALT key is pressed. – Jimi Jul 15 '21 at 02:30
  • 1
    I mean, you can check `ModifierKeys == (Keys.Control | Keys.Alt)`. – Jimi Jul 15 '21 at 02:36

1 Answers1

1

here is the solution by https://stackoverflow.com/users/7444103/jimi

 if (Control.ModifierKeys.ToString() == "Alt" || Control.ModifierKeys == (Keys.Control | Keys.Alt))
        {
            MessageBox.Show("The Alt key is being pressed");
        }