简体   繁体   English

如何在 C/C++ 程序中使用键盘的特殊键,例如 Command、Shift、Alt、Tab 或功能键?

[英]How to use the special keys of the keyboard such as Command, Shift, Alt, Tab or the Function-Keys inside a C/C++-program?

My Problem is the following:我的问题如下:

I want to make a program, a simple text-editor, which can work with all keys of a keyboards set such as the Function Keys (F1 to F12), Command, Left-Shift, ALT or TAB.我想制作一个程序,一个简单的文本编辑器,它可以使用键盘集的所有键,例如 Function 键(F1 到 F12)、Command、Left-Shift、ALT 或 TAB。

More precise, I want to be able to assign all keys to specific operations.更准确地说,我希望能够将所有键分配给特定操作。

I have made a program to print out the ASCII values of those keys, but the keys doesn´t get accepted as i want to, my test program prints out either values of several ASCII codes or doesn´t accept it all like by pressing the Command Key, instead:我已经编写了一个程序来打印出这些键的 ASCII 值,但是这些键并没有被我想要的那样接受,我的测试程序打印出几个 ASCII 码的值或者不接受它,就像按下命令键,而不是:

My test-program to print out the ASCII-value of a pressed key:我的测试程序打印出按键的 ASCII 值:

#include <stdio.h> 

int main (void)
{
    char k;

    printf("\n\n\n\n");

    while(1)
    {

       printf("Please press any key:");

       k = getchar();
       getchar();

       printf("ASCII code of given input key: %d",k);
       printf("\n\n\n");
    }
}

Output after pressing on F2-Key: Output 按下 F2 键后:

Please press any key:^[OQ
ASCII code of given input key: 27


Please press any key:ASCII code of given input key: 81

Output after pressing on F2-Key: Output 按下 F2 键后:

Please press any key:^[[15~
ASCII code of given input key: 27


Please press any key:ASCII code of given input key: 49


Please press any key:ASCII code of given input key: 126

Output after pressing on Command-,Alt- or Left-Shift-Key: Output 按下 Command-、Alt- 或 Left-Shift-Key 后:

Please press any key:               // no reaction at all

Due to the fact, that i think this must be done easily, because of course its normal to use all keys of a keyboard within a program, it isn´t quite so easy for me as a newbie to find the proper way to achieve that.由于事实上,我认为这必须很容易完成,因为在程序中使用键盘的所有键当然是正常的,对于我作为新手来说,找到实现这一目标的正确方法并不是那么容易.

I have found that question here on Stackoverflow but it belongs to C#: Special keys on keyboards我在 Stackoverflow 上发现了这个问题,但它属于 C#: 键盘上的特殊键

Why i can´t fetch an ASCII value of those keys?为什么我不能获取这些键的 ASCII 值?

Sorry, if i might have an mistaken impression that these keys belong to ASCII values.抱歉,如果我可能错误地认为这些键属于 ASCII 值。

Access to "special keys" is not part of the standard library because the language does not assume any specific I/O hardware such as a screen and keyboard, it only assumes standard character based I/O streams, and the data on these streams relate to characters, not keys.访问“特殊键”不是标准库的一部分,因为该语言不假设任何特定的 I/O 硬件,例如屏幕和键盘,它只假设基于标准字符的 I/O 流,并且这些流上的数据相关到字符,而不是键。

As such receiving keyboard events and scan-codes and platform specific.因此接收键盘事件和扫描码以及特定平台。 However there are cross-platform mean by which you can get some commonality across systems that support a keyboard (such as desktop computers - not everything C runs on is a desktop computer).但是,通过跨平台的方式,您可以在支持键盘的系统之间获得一些共性(例如台式计算机 - 并非 C 运行的所有东西都是台式计算机)。

For example for text console applications such as in your example you might use the ncurses library which is a clone of the Unix curses library, and has a Windows near equivalent in the PDcurses library.例如,对于您的示例中的文本控制台应用程序,您可以使用 ncurses 库,它是 Unix 诅咒库的克隆,并且在 PDcurses 库中具有几乎等效的 Windows。

In these curses variants for example, you can write:例如,在这些 curses 变体中,您可以编写:

int ch = getch() ;
if(ch == KEY_LEFT)
    printw("Left arrow is pressed\n");

Under the hood it is a wrapper for the platform specific means.在引擎盖下,它是平台特定方式的包装器。 If cross-platform is not an issue you may utilise these means directly.如果跨平台不是问题,您可以直接使用这些方法。 For example MSVC++ has a conio library, which has a _getch() function similar to that above, except that for non-character keys, it returns 0x00 or 0xE0, and returns teh key code when called a second time.例如 MSVC++ 有一个 conio 库,它有一个_getch() function 类似于上面的,除了对于非字符键,它返回 0x00 或 0xE0,并在第二次调用时返回键码。

For GUI applications, the applications message loop will receive keyboard events, the exact means will vary between systems, but again there are cross-platform solutions such as Qt or WxWidgets for example.对于 GUI 应用程序,应用程序消息循环将接收键盘事件,具体方式因系统而异,但同样有跨平台解决方案,例如 Qt 或 WxWidgets。

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

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