getwch`返回的键码是什么?

时间:2019-04-27 00:20:16

标签: python windows visual-c++ keycode

我正在使用msvcrt.getwch在针对Windows的Python中进行交互式提示。

文档说:

  

读取按键并以字节字符串形式返回结果字符。什么也没有回应到控制台。如果尚未提供按键,则该呼叫将被阻止,但不会等待按下Enter键。如果按下的键是特殊功能键,则将返回“ \ 000”或“ \ xe0”;下一次调用将返回键码。使用此功能无法读取Control-C按键。

我发现F10使用前者\x00D,而F11使用后\xe0\x85

但是,这并不能说明键码是什么。 这很重要,因为除其他键之外,我没有\xe0键(à),因此无法测试输入此值的输出。


其他方面

例如,我尝试通过两种方式将此键粘贴到提示中。

  • CTRL-v-输入密钥代码\x16。这是ASCII中的synchronous idle键。 [1] (这是我输入的组合键。)
  • 右键单击(在PS中粘贴)-仅输入\xe0

    我不确定Power Shell和Visual C ++是否允许我粘贴转义序列,或者这是对该键的实际处理。


鉴于msvcrt似乎是Visual C ++的包装,我也查看了该文档。

  

_getch和_getwch函数从控制台读取单个字符,而不回显该字符。这些功能均不能用于读取CTRL + C。读取功能键或箭头键时,每个功能必须调用两次;第一次调用返回0或0xE0,第二次调用返回实际的密钥代码。

这也没有说出键码是什么。

我已经上网了,但是我只能找到有关virtual keycodes的信息,但是这说F10是\x79,而F11是\x7A。由于此函数未返回这些值,因此我知道它没有引用这些键码。

2 个答案:

答案 0 :(得分:5)

仅在Visual c ++的帮助下对此做出响应。

功能 getwch 将返回扫描代码而不是虚拟键代码。 因此对virtual keycodes的引用无效。

此外,根据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
}

为回答您的问题,Keyboard Scan Code Specification中列出了有效的扫描代码。

例如,上述函数的第二次调用将为F10键返回68(0x44),并且在MSDN中也是如此: F10 scan code

Key Scan Codes中存在相同的列表,但是由于文档较旧而无法通过身份验证。

答案 1 :(得分:-2)

不幸的是,另一个答案中提供的scan codes存在一些问题,缺少功能,错误的值和其他功能。

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

要查看该表,您必须运行以下代码段。 This is as SE refuse to add MathJax to 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}
$$