为什么我无法阅读所有 Ctrl + "字母"
Why can't I read all Ctrl + 'letters'
我制作了一个程序,可以读取键盘上的所有独立功能键(至少我想测试一下)。我设计了它,这样我就可以将任何单个键输入称为单个值。它处理返回、F1-12、删除格 我只是想测试输入的修改。我已经确保了shift的工作,但现在我决定测试Ctrl和Alt 问题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。这两种方式都可以让您从较低级别查看键盘输入。
- 输入中的字符串数未知(以字母表示)
- 将数字转换为字母(例如:123 转换为一二三)
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 按字母顺序对字符串中的字母进行排序,而无需使用内置的 sort()
- 按字母顺序对C++问题中的子字符串索引进行分区
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何在 c++ 中将所有大写字母转换为字符串中的小写字母,反之亦然?
- 如何在C++中移动字母表中的字母?
- 大写字母
- 如何修复艺术ASCII,我点击一个字母就可以了,但输入一个阶段艺术出来了
- 如何计算特定字母在字符串中出现的次数?(C++)
- 将字符串中的 int 转换为字母;
- Ctrl + 不适用于在 Visual Studio 中运行该程序
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 为什么我无法阅读所有 Ctrl + "字母"