简体   繁体   English

C# Alt 键问题

[英]C# Alt Key problems

I have some questions regarding the use of the Alt key in C#.我有一些关于在 C# 中使用 Alt 键的问题。

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.如果我理解正确, Keys.Alt 用于组合键,而 Keys.Menu 用于检测 Alt 键的简单按下。 I am not looking to combine keys, so I'm using Keys.Menu.我不想组合键,所以我使用 Keys.Menu。 I do find it strange that for Shift, there is a Keys.ShiftKey, but for Alt it's called Keys.Menu.我确实觉得奇怪的是,对于 Shift,有一个 Keys.ShiftKey,但对于 Alt,它被称为 Keys.Menu。 Why not just call it Keys.AltKey?为什么不直接称其为 Keys.AltKey? Confusing!令人困惑!

Anyway, the code is working correctly.无论如何,代码工作正常。 However, there seems to be a default function of the Alt key.但是,似乎有 Alt 键的默认功能。 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.例如,当我在文档或其他内容中键入内容并按 Alt 时,插入点光标消失,就像取消选择当前窗口或任务一样。

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: Alt键的这个默认功能对我的程序有问题,所以我做了一些搜索并找到了以下代码以禁用它:

    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.输入此代码后,Alt 键的默认功能已被禁用。 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.然而,似乎 Alt 键也变得完全不起作用,不再像我编程的那样执行((做某事))的任务。

Is there any way to disable the Alt key's default function without causing it to become a non-functional key?有什么方法可以禁用 Alt 键的默认功能而不使其成为非功能键?

Without knowing more of what you're trying to do here...不知道更多你想在这里做什么......

...why can't you execute your "something" before returning true? ...为什么你不能在返回 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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