简体   繁体   中英

C# Alt Key problems

I have some questions regarding the use of the Alt key in C#.

Here is my code:

switch (e.KeyCode)
{
    case Keys.Menu:
        ((do something))
        break;
}

If I understand correctly, Keys.Alt is used for key combinations, while Keys.Menu is used for detecting simple presses of the Alt key. I am not looking to combine keys, so I'm using Keys.Menu. I do find it strange that for Shift, there is a Keys.ShiftKey, but for Alt it's called Keys.Menu. Why not just call it Keys.AltKey? Confusing!

Anyway, the code is working correctly. However, there seems to be a default function of the Alt key. For example, when I am typing something in a document or whatever and I press Alt, the insertion point cursor disappears, as if the current window or task was deselected.

This default function of the Alt key is problematic to my program, so I've done some searching and found the following code in order to disable it:

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if ((keyData & Keys.Alt) == Keys.Alt)
            return true;
        else
            return base.ProcessDialogKey(keyData);
    }

With this code put in, the Alt key's default function has been disabled. However, it seems that the Alt key has also become completely non-functional and no longer performs the task of ((doing something)) like I had programmed it to do.

Is there any way to disable the Alt key's default function without causing it to become a non-functional key?

Without knowing more of what you're trying to do here...

...why can't you execute your "something" before returning true?

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if ((keyData & Keys.Alt) == Keys.Alt)
        {
            Console.WriteLine("Alt");

            // ... call something from right here! ...

            return true;
        }

        return base.ProcessDialogKey(keyData);
    }

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