`getwch`函数返回的关键码是什么?

7
我正在使用msvcrt.getwch在Python中制作一个针对Windows的交互式提示符。
文档说明如下:
读取按键并返回结果字符作为字节字符串。不会在控制台上回显任何内容。如果没有按键可用,则此调用将阻塞,但不会等待按Enter键。如果按下的键是特殊功能键,则此函数将返回'\000'或'\xe0';下一次调用将返回键码。无法使用此函数读取Control-C按键。
我已经发现F10使用前者-\x00D,而F11使用后者\xe0\x85
然而,这并未说明键码是什么。这很重要,因为我没有\xe0键(à),还有其他键,因此无法测试输入此值的输出。

额外的侧面背景

例如,我尝试以两种方式将此密钥粘贴到提示符中。

  • CTRL-v - 这会输入键码\x16。这是ASCII中的 同步空闲键。 [1](这是我输入的键组合。)
  • 右键单击(在PS中粘贴)- 结果只输入\xe0

    我不确定Power Shell和Visual C++是否让我粘贴转义序列,还是实际处理该键。


考虑到msvcrt似乎是Visual C++的包装器,我也查看了相关文档。
_getch和_getwch函数从控制台读取单个字符而不回显该字符。这些函数中没有一个可以用于读取CTRL+C。当读取功能键或箭头键时,必须调用每个函数两次;第一次调用返回0或0xE0,第二次调用返回实际的键码。
这也没有说明键码是什么。
我在网上搜索过,但只能找到有关虚拟键码的信息,然而这表明F10是\x79,F11是\x7A。由于此函数没有返回这些值,我知道它不是指这些键码。
2个回答

5

我将仅使用Visual C++帮助您进行回复。

函数getwch将返回扫描代码而不是虚拟键代码。因此,参考虚拟键码是无效的。

另外,根据MSDN _getch, _getwch文档,当读取功能键或箭头键时,每个函数必须调用两次;第一次调用返回0或0xE0,第二次调用返回实际的键代码。

以下代码将返回有效的扫描代码:

#include<stdio.h>
#include <ctype.h>

int main()
{
    wchar_t ch = _getwch();  //Will return '0'
    ch = _getwch();          //Will return valid scan code
}

回答您的问题,有效扫描码的列表在“ 键盘扫描码规范”中。
例如,上述函数的第二次调用将返回68(0x44)表示按下F10键,并且在MSDN中也是相同的: F10 scan code 相同的列表也包含在键扫描代码中,但由于这是旧文档,无法进行验证。

你说的“引用[...] ASCII 码是无效的”是什么意思?文档明确说明它输出它们,“正常的 API 只处理 ASCII 字符”。为了非常清楚,按下 CTRL-v 返回一个非转义的纯 ASCII 值,\x16。它没有前缀 \x00\xe0 - Peilonrayz
除此之外,这些文档看起来都正确,我已经验证了大部分我从旧的键扫描码页得到的值。 - Peilonrayz

-2

不幸的是,其他答案中提供的扫描代码存在一些问题,缺少功能、值不正确和额外的功能。

为了帮助其他使用此函数的人,以下是我的计算机使用getwch所展示的功能。

要查看表格,您需要运行以下代码片段。这是因为SE拒绝将MathJax添加到SO

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML-full"></script> <script type="text/x-mathjax-config"> MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts:["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk:(MathJax.Hub.Browser.isMobile ? 10 : 50) }, tex2jax: { inlineMath: [ ["$", "$"], ["\\\\(","\\\\)"] ], displayMath: [ ["$$","$$"], ["\\[", "\\]"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno" }, TeX: { noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } }, messageStyle: "none" });   </script>

$\text{Key:}$  <br>
$\text{Documented and works}$  <br>
$\color{red}{\text{Missing functionality}}$  <br>
$\color{green}{\text{Additional functionality}}$  <br>
$\color{gray}{\text{Untested}}$  <br>
$\to \text{Changed functionality}$  <br>

<b>$\text{Note}$</b>: $\text{\xe0} = \text{à}$  <br>

$$
\begin{array}{l|r|l|l|l|l}
  \textrm{Name} &
  \textrm{Scan Code} &
  \textrm{Base} &
  \textrm{Shift} &
  \textrm{Ctrl} &
  \textrm{Alt} \\
  \hline
  
  \text{ESC}&\color{gray}{\text{1}}&\text{\x1b}&\text{\x1b}&\color{red}{\text{\x1b}}&\color{red}{\text{\x00\x01}}\\
  \text{1!}&\color{gray}{\text{2}}&\text{1}&\text{!}&&\color{red}{\text{\x00x}} \to \color{green}{\text{1}}\\
  \text{2@}&\color{gray}{\text{3}}&\text{2}&\text{@}&\text{\x00\x03}&\color{red}{\text{\x00y}} \to \color{green}{\text{2}}\\
  \text{3#}&\color{gray}{\text{4}}&\text{3}&\text{#}&&\color{red}{\text{\x00z}} \to \color{green}{\text{3}}\\
  \text{4\$}&\color{gray}{\text{5}}&\text{4}&\text{\$}&&\color{red}{\text{\x00\{}} \to \color{green}{\text{4}}\\
  \text{5%}&\color{gray}{\text{6}}&\text{5}&\text{%}&&\color{red}{\text{\x00|}} \to \color{green}{\text{5}}\\
  \text{6^}&\color{gray}{\text{7}}&\text{6}&\text{^}&\color{red}{\text{\x1e}}&\color{red}{\text{\x00\}}} \to \color{green}{\text{6}}\\
  \text{7&}&\color{gray}{\text{8}}&\text{7}&\text{&}&&\color{red}{\text{\x00~}} \to \color{green}{\text{7}}\\
  \text{8*}&\color{gray}{\text{9}}&\text{8}&\text{*}&&\color{red}{\text{\x00\x7f}} \to \color{green}{\text{8}}\\
  \text{9(}&\color{gray}{\text{10}}&\text{9}&\text{(}&&\color{red}{\text{\x00\x80}} \to \color{green}{\text{9}}\\
  \text{0)}&\color{gray}{\text{11}}&\text{0}&\text{)}&&\color{red}{\text{\x00\x81}} \to \color{green}{\text{0}}\\
  \text{-_}&\color{gray}{\text{12}}&\text{-}&\text{_}&\color{red}{\text{\x1f}}&\color{red}{\text{\x00\x82}} \to \color{green}{\text{-}}\\
  \text{=+}&\color{gray}{\text{13}}&\text{=}&\text{+}&&\color{red}{\text{\x00\x83}} \to \color{green}{\text{=}}\\
  \text{BKSP}&\color{gray}{\text{14}}&\text{\x08}&\text{\x08}&\text{\x7f}&\color{red}{\text{\x00\x0e}} \to \color{green}{\text{\x08}}\\
  \text{TAB}&\color{gray}{\text{15}}&\text{\t}&\color{red}{\text{\x00\x0f}} \to \color{green}{\text{\t}}&\text{\x00\x94}&\color{red}{\text{\x00\x0f}}\\
  \text{Q}&\color{gray}{\text{16}}&\text{q}&\text{Q}&\text{\x11}&\color{red}{\text{\x00\x10}} \to \color{green}{\text{q}}\\
  \text{W}&\color{gray}{\text{17}}&\text{w}&\text{W}&\text{\x17}&\color{red}{\text{\x00\x11}} \to \color{green}{\text{w}}\\
  \text{E}&\color{gray}{\text{18}}&\text{e}&\text{E}&\text{\x05}&\color{red}{\text{\x00\x12}} \to \color{green}{\text{e}}\\
  \text{R}&\color{gray}{\text{19}}&\text{r}&\text{R}&\text{\x12}&\color{red}{\text{\x00\x13}} \to \color{green}{\text{r}}\\
  \text{T}&\color{gray}{\text{20}}&\text{t}&\text{T}&\text{\x14}&\color{red}{\text{\x00\x14}} \to \color{green}{\text{t}}\\
  \text{Y}&\color{gray}{\text{21}}&\text{y}&\text{Y}&\text{\x19}&\color{red}{\text{\x00\x15}} \to \color{green}{\text{y}}\\
  \text{U}&\color{gray}{\text{22}}&\text{u}&\text{U}&\text{\x15}&\color{red}{\text{\x00\x16}} \to \color{green}{\text{u}}\\
  \text{I}&\color{gray}{\text{23}}&\text{i}&\text{I}&\text{\t}&\color{red}{\text{\x00\x17}} \to \color{green}{\text{i}}\\
  \text{O}&\color{gray}{\text{24}}&\text{o}&\text{O}&\text{\x0f}&\color{red}{\text{\x00\x18}} \to \color{green}{\text{o}}\\
  \text{P}&\color{gray}{\text{25}}&\text{p}&\text{P}&\text{\x10}&\color{red}{\text{\x00\x19}} \to \color{green}{\text{p}}\\
  \text{[\{}&\color{gray}{\text{26}}&\text{[}&\text{\{}&\text{\x1b}&\color{red}{\text{\x00\x1a}} \to \color{green}{\text{[}}\\
  \text{]\}}&\color{gray}{\text{27}}&\text{]}&\text{\}}&\text{\x1d}&\color{red}{\text{\x00\x1b}} \to \color{green}{\text{]}}\\
  \text{ENTER}&\color{gray}{\text{28}}&\text{\r}&\text{\r}&\text{\n}&\color{red}{\text{\x00\x1c}}\\
  \text{ENTER£}&\color{gray}{\text{28}}&\text{\r}&\text{\r}&\text{\n}&\color{red}{\text{\x00¦}}\\
  \text{LCTRL}&\color{gray}{\text{29}}&&&&\\
  \text{RCTRL£}&\color{gray}{\text{29}}&&&&\\
  \text{A}&\color{gray}{\text{30}}&\text{a}&\text{A}&\text{\x01}&\color{red}{\text{\x00\x1e}} \to \color{green}{\text{a}}\\
  \text{S}&\color{gray}{\text{31}}&\text{s}&\text{S}&\text{\x13}&\color{red}{\text{\x00\x1f}} \to \color{green}{\text{s}}\\
  \text{D}&\color{gray}{\text{32}}&\text{d}&\text{D}&\text{\x04}&\color{red}{\text{\x00 }} \to \color{green}{\text{d}}\\
  \text{F}&\color{gray}{\text{33}}&\text{f}&\text{F}&\text{\x06}&\color{red}{\text{\x00!}} \to \color{green}{\text{f}}\\
  \text{G}&\color{gray}{\text{34}}&\text{g}&\text{G}&\text{\x07}&\color{red}{\text{\x00"}} \to \color{green}{\text{g}}\\
  \text{H}&\color{gray}{\text{35}}&\text{h}&\text{H}&\text{\x08}&\color{red}{\text{\x00#}} \to \color{green}{\text{h}}\\
  \text{J}&\color{gray}{\text{36}}&\text{j}&\text{J}&\text{\n}&\color{red}{\text{\x00\$}} \to \color{green}{\text{j}}\\
  \text{K}&\color{gray}{\text{37}}&\text{k}&\text{K}&\text{\x0b}&\color{red}{\text{\x00%}} \to \color{green}{\text{k}}\\
  \text{L}&\color{gray}{\text{38}}&\text{l}&\text{L}&\text{\x0c}&\color{red}{\text{\x00&}} \to \color{green}{\text{l}}\\

\end{array}
$$


$$
\begin{array}{l|r|l|l|l|l}
  \textrm{Name} &
  \textrm{Scan Code} &
  \textrm{Base} &
  \textrm{Shift} &
  \textrm{Ctrl} &
  \textrm{Alt} \\
  \hline
  
  \text{;:}&\color{gray}{\text{39}}&\text{;}&\text{:}&&\color{red}{\text{\x00'}} \to \color{green}{\text{;}}\\
  \text{'"}&\color{gray}{\text{40}}&\text{'}&\text{"}&&\color{red}{\text{\x00(}} \to \color{green}{\text{'}}\\
  \text{`~}&\color{gray}{\text{41}}&\text{`}&\text{~}&&\color{red}{\text{\x00)}} \to \color{green}{\text{`}}\\
  \text{L SHIFT}&\color{gray}{\text{42}}&&&&\\
  \text{\\|}&\color{gray}{\text{43}}&\text{\\}&\text{|}&\text{\x1c}&\color{green}{\text{\\}}\\
  \text{Z}&\color{gray}{\text{44}}&\text{z}&\text{Z}&\text{\x1a}&\color{red}{\text{\x00,}} \to \color{green}{\text{z}}\\
  \text{X}&\color{gray}{\text{45}}&\text{x}&\text{X}&\text{\x18}&\color{red}{\text{\x00-}} \to \color{green}{\text{x}}\\
  \text{C}&\color{gray}{\text{46}}&\text{c}&\text{C}&\text{\x03}&\color{red}{\text{\x00.}} \to \color{green}{\text{c}}\\
  \text{V}&\color{gray}{\text{47}}&\text{v}&\text{V}&\text{\x16}&\color{red}{\text{\x00/}} \to \color{green}{\text{v}}\\
  \text{B}&\color{gray}{\text{48}}&\text{b}&\text{B}&\text{\x02}&\color{red}{\text{\x000}} \to \color{green}{\text{b}}\\
  \text{N}&\color{gray}{\text{49}}&\text{n}&\text{N}&\text{\x0e}&\color{red}{\text{\x001}} \to \color{green}{\text{n}}\\
  \text{M}&\color{gray}{\text{50}}&\text{m}&\text{M}&\text{\r}&\color{red}{\text{\x002}} \to \color{green}{\text{m}}\\
  \text{,<}&\color{gray}{\text{51}}&\text{,}&\text{<}&&\color{red}{\text{\x003}} \to \color{green}{\text{,}}\\
  \text{.>}&\color{gray}{\text{52}}&\text{.}&\text{>}&&\color{red}{\text{\x004}} \to \color{green}{\text{.}}\\
  \text{/?}&\color{gray}{\text{53}}&\text{/}&\text{?}&&\color{red}{\text{\x005}} \to \color{green}{\text{/}}\\
  \text{GRAY/£}&\color{gray}{\text{53}}&\color{gray}{\text{/}}&\color{gray}{\text{?}}&\color{gray}{\text{\x00\x95}}&\color{gray}{\text{\x00¤}}\\
  \text{R SHIFT}&\color{gray}{\text{54}}&&&&\\
  \text{PRISC}&\color{gray}{\text{55}}&\color{gray}{\text{*}}&\color{gray}{\text{PRISC}}&\color{gray}{\text{\x10}}&\\
  \text{L ALT}&\color{gray}{\text{56}}&&&&\\
  \text{R ALT£}&\color{gray}{\text{57}}&&&&\\
  \text{SPACE}&\color{gray}{\text{57}}&\text{ }&\text{ }&\text{ }&\color{red}{\text{ }}\\
  \text{CAPS}&\color{gray}{\text{58}}&&&&\\
  \text{F1}&\color{gray}{\text{59}}&\text{\x00;}&\text{\x00T}&\text{\x00^}&\text{\x00h}\\
  \text{F2}&\color{gray}{\text{60}}&\text{\x00<}&\text{\x00U}&\text{\x00_}&\text{\x00i}\\
  \text{F3}&\color{gray}{\text{61}}&\text{\x00=}&\text{\x00V}&\text{\x00`}&\text{\x00j}\\
  \text{F4}&\color{gray}{\text{62}}&\text{\x00>}&\text{\x00W}&\text{\x00a}&\text{\x00k}\\
  \text{F5}&\color{gray}{\text{63}}&\text{\x00?}&\text{\x00X}&\text{\x00b}&\text{\x00l}\\
  \text{F6}&\color{gray}{\text{64}}&\text{\x00@}&\text{\x00Y}&\text{\x00c}&\text{\x00m}\\
  \text{F7}&\color{gray}{\text{65}}&\text{\x00A}&\text{\x00Z}&\text{\x00d}&\text{\x00n}\\
  \text{F8}&\color{gray}{\text{66}}&\text{\x00B}&\text{\x00[}&\text{\x00e}&\text{\x00o}\\
  \text{F9}&\color{gray}{\text{67}}&\text{\x00C}&\text{\x00\\}&\text{\x00f}&\text{\x00p}\\
  \text{F10}&\color{gray}{\text{68}}&\text{\x00D}&\text{\x00]}&\text{\x00g}&\text{\x00q}\\
  \text{F11£}&\color{gray}{\text{87}}&\text{à\x85}&\text{à\x87}&\text{à\x89}&\text{à\x8b}\\
  \text{F12£}&\color{gray}{\text{88}}&\text{à\x86}&\text{à\x88}&\text{à\x8a}&\text{à\x8c}\\
  \text{NUM}&\color{gray}{\text{69}}&&&&\\
  \text{HOME}&\color{gray}{\text{71}}&\text{\x00G}&\color{red}{\text{7}}&\text{\x00w}&\\
  &&\color{green}{\text{7}}&\color{green}{\text{\x00G}}&\color{green}{\text{\x00w}}&\\
  \text{HOME£}&\color{gray}{\text{71}}&\text{àG}&\text{àG}&\text{àw}&\text{\x00\x97}\\
  \text{UP}&\color{gray}{\text{72}}&\text{\x00H}&\color{red}{\text{8}}&\text{\x00\x8d}&\\
  &&\color{green}{\text{8}}&\color{green}{\text{\x00H}}&\color{green}{\text{\x00\x8d}}&\\

\end{array}
$$


$$
\begin{array}{l|r|l|l|l|l}
  \textrm{Name} &
  \textrm{Scan Code} &
  \textrm{Base} &
  \textrm{Shift} &
  \textrm{Ctrl} &
  \textrm{Alt} \\
  \hline
  
  \text{UP£}&\color{gray}{\text{72}}&\text{àH}&\text{àH}&\text{à\x8d}&\text{\x00\x98}\\
  \text{PGUP}&\color{gray}{\text{73}}&\text{\x00I}&\color{red}{\text{9}}&\text{\x00\x84}&\color{red}{\text{\x00\x99}}\\
  &&\color{green}{\text{9}}&\color{green}{\text{\x00I}}&\color{green}{\text{\x00\x84}}&\\
  \text{PGUP£}&\color{gray}{\text{73}}&\text{àI}&\text{àI}&\color{red}{\text{à\x84}} \to \color{green}{\text{à\x86}}&\text{\x00\x99}\\
  \text{GRAY-}&\color{gray}{\text{74}}&&\color{gray}{\text{-}}&&\\
  \text{LEFT}&\color{gray}{\text{75}}&\text{\x00K}&\color{red}{\text{4}}&\text{\x00s}&\\
  &&\color{green}{\text{4}}&\color{green}{\text{\x00K}}&\color{green}{\text{\x00s}}&\\
  \text{LEFT£}&\color{gray}{\text{75}}&\text{àK}&\text{àK}&\text{às}&\text{\x00\x9b}\\
  \text{CENTER}&\color{gray}{\text{76}}&&\color{red}{\text{5}}&&\\
  &&\color{green}{\text{5}}&&&\\
  \text{RIGHT}&\color{gray}{\text{77}}&\text{\x00M}&\color{red}{\text{6}}&\text{\x00t}&\\
  &&\color{green}{\text{6}}&\color{green}{\text{\x00M}}&\color{green}{\text{\x00t}}&\\
  \text{RIGHT£}&\color{gray}{\text{77}}&\text{àM}&\text{àM}&\text{àt}&\text{\x00\x9d}\\
  \text{GRAY+}&\color{gray}{\text{78}}&&\color{gray}{\text{+}}&&\\
  \text{END}&\color{gray}{\text{79}}&\text{\x00O}&\color{red}{\text{1}}&\text{\x00u}&\\
  &&\color{green}{\text{1}}&\color{green}{\text{\x00O}}&\color{green}{\text{\x00u}}&\\
  \text{END£}&\color{gray}{\text{79}}&\text{àO}&\text{àO}&\text{àu}&\text{\x00\x9f}\\
  \text{DOWN}&\color{gray}{\text{80}}&\text{\x00P}&\color{red}{\text{2}}&\text{\x00\x91}&\\
  &&\color{green}{\text{2}}&\color{green}{\text{\x00P}}&\color{green}{\text{\x00\x91}}&\\
  \text{DOWN£}&\color{gray}{\text{80}}&\text{àP}&\text{àP}&\text{à\x91}&\text{\x00\xa0}\\
  \text{PGDN}&\color{gray}{\text{81}}&\text{\x00Q}&\color{red}{\text{3}}&\text{\x00v}&\\
  &&\color{green}{\text{3}}&\color{green}{\text{\x00Q}}&\color{green}{\text{\x00v}}&\\
  \text{PGDN£}&\color{gray}{\text{81}}&\text{àQ}&\text{àQ}&\text{àv}&\text{\x00¡}\\
  \text{INS}&\color{gray}{\text{82}}&\text{\x00R}&\color{red}{\text{0}}&\text{\x00\x92}&\\
  &&\color{green}{\text{0}}&\color{green}{\text{\x00R}}&\color{green}{\text{\x00\x92}}&\\
  \text{INS£}&\color{gray}{\text{82}}&\text{àR}&\text{àR}&\text{à\x92}&\text{\x00¢}\\
  \text{DEL}&\color{gray}{\text{83}}&\text{\x00S}&\color{red}{\text{.}}&\text{\x00\x93}&\\
  &&\color{green}{\text{.}}&\color{green}{\text{\x00S}}&\color{green}{\text{\x00\x93}}&\\
  \text{DEL£}&\color{gray}{\text{83}}&\text{àS}&\text{àS}&\text{à\x93}&\text{\x00£}\\

\end{array}
$$


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接