0

キーボード上のすべてのスタンドアロン ファンクション キー (少なくともテストしようと思っていたもの) を読み取ることができるプログラムを作成しました。単一のキー入力を単一の値として参照できるように設計しました。ReturnF1- F12deletebackspace、矢印などを処理します

入力の変更をテストすることを考えました。シフトが機能することは既に確認しましたが、今度は と をテストすることにしましCtrlAlt

質問 1Alt入力キー コードを変更しないのは なぜですか?

質問 2Ctrl特定の+ の組み合わせ をキャプチャできないのはなぜですか? 例えば。Ctrl+ s; Ctrl+ 1- 9;

Ctrl+2動作しますが、キーボードが UK に設定されていることが原因である可能性があります。

これは私が使用しているコードです。

これらのキーの組み合わせをキャプチャする方法を必ずしも尋ねているわけではないことに注意してください (1 つまたは 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
    }
}
4

1 に答える 1

0

これは、環境に非常に固有です。conioDOS / Windowsに固有のものを 使用しています。

+ alpha キー値のほとんどはCtrl文字 1 ~ 26 にバインドされ、その他の一部は 31 未満の値にバインドされて、ASCII 制御文字にマップされます。しかし、Ctrl+のSように特別な意味を持つものもあり ( Ctrl+Sは ASCII では XOFF です)、環境によって「食べられる」可能性があります。

基本的に、あなたが直面している問題はgetch、古い学校のシリアル端末インターフェイスに似ているという事実です。これらは、修飾キーなどを区別し、ファンクション キーなどの特殊キーをより適切に処理できるようにする下位レベルとは対照的に、「最小公分母」レベルでキーボード イベントのみを公開します。

(お気づきのように、ファンクション キーには特別なマルチバイト シーケンスがあります。これも、キーボードがリモート リンクの反対側にある古い学校のシリアル端末をエミュレートするためです。)

低レベルの (したがって、より直接的で柔軟なインターフェイス) を取得するには、よりプラットフォーム固有のライブラリ、または SDL などのより豊富なライブラリを使用する必要があります。どちらも、キーボードからの入力の下位レベルのビューを提供します。

于 2013-11-20T04:30:15.980 に答える