为什么我无法阅读所有 Ctrl + "字母"

Why can't I read all Ctrl + 'letters'

本文关键字:Ctrl 字母 为什么      更新时间:2023-10-16

我制作了一个程序,可以读取键盘上的所有独立功能键(至少我想测试一下)。我设计了它,这样我就可以将任何单个键输入称为单个值。它处理返回F1-12删除格

我只是想测试输入的修改。我已经确保了shift的工作,但现在我决定测试CtrlAlt

问题1为什么Alt不修改任何输入键代码?

问题2为什么我不能捕获某些Ctrl+组合?例如Ctrl+sCtrl+1-9

Ctrl+2有效,但我认为这可能是因为我的键盘设置为英国。

这是我正在使用的代码。

请注意,我不一定要问如何捕捉这些组合键(除非是一两个简单的修改)。我只想知道为什么我不能。

#include <iostream>
#include <conio.h>
#include <cwchar>
union wide_char
{
    short Result;
    char C[2];
}; 
int main()
{
    wchar_t R;
    int N;
    wide_char user_input;
                                                    //Loops forever, this is only a proof of concept program proving this is possible to incorporate into a larger program
    while(true)
    {
        user_input.C[0] = 0;
        user_input.C[1] = 0;
                                                    //Loop twice, or until code causes the loop to exit
                                                        //Two times are neccessary for function keys unfortunately
        for(int i = 0; i < 2; ++i)
        {
                                                    //While there isn't a key pressed, loop doing nothing
            while(!kbhit()){}
                                                    //Grab the next key from the buffer
                                                        //Since the loop is done, there must be at least one
            user_input.C[i] = getch();
            switch(user_input.C[i])
            {
                case 0:
                case -32:
                                                    //The key pressed is a Function key because it matches one of these two cases
                                                        //This means getch() must be called twice
                                                        //Break switch, run the for loop again ++i
                    break;
                default:
                                                    //The character obtained from getch() is from a regular key
                                                        //Or this is the second char from getch() because the for loop is on run #2
                                                        //Either way we need a wide char (16 bits / 2 Bytes)
                    if(user_input.C[1] != 0)
                                                    //Function keys {Arrows, F1-12, Esc}
                                                    //We now combine the bits of both chars obtained
                                                        //They must be combined Second+First else the F1-12 will be duplicate
                                                        //This is because on F1-12 getch() returns 0 thus won't affect the combination
                        R = user_input.Result;
                    else
                                                    //Regular key press
                        R = user_input.C[0];
                                                    //Display our unique results from each key press
                    N = R;
                    std::cout << R << " R = N " << N << std::endl;
                    if( R == 'a' )
                        std::cout << "a = " << N << std::endl;
                                                    //Manually break for loop
                    i = 3;
                    break;
            }
        }
                                                    //We need to reset the array in this situation
                                                        //Else regular key presses will be affected by the last function key press
    }
}

这是非常特定于您的环境的。您使用的是特定于DOS/Windows的conio

大多数Ctrl+alpha键值都绑定到字符1-26,而某些其他键值则绑定到31以下的其他值,以映射到ASCII控制字符。但有些,如Ctrl+S具有特殊含义(<kbd]Ctrl>+S在ASCII中是XOFF),因此可能会被您的环境"吃掉"。

从根本上讲,您面临的问题是getch近似于老式串行终端接口。它们只在"最小公分母"级别上公开键盘事件,而不是在较低级别上公开,这样可以区分修改键等,并为处理特殊键(如功能键)提供更好的方法。

(正如你所注意到的,功能键有特殊的多字节序列。同样,这是由于模拟老式串行终端,键盘可能在远程链接的另一端。)

要获得较低级别(因此更直接、更灵活的界面),您需要使用更特定于平台的库,或更丰富的库,如SDL。这两种方式都可以让您从较低级别查看键盘输入。