🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
关于如何在VC++程序中自定义热键(或称组合键)及其响应,我在之前的博客中已经作了详细的介绍,那么当遇到的是单个键盘按键的响应时,我们又该怎么解决呢?这时,我们可以采用afx_msg BOOL PreTranslateMessage (MSG* pMsg) 这个函数很方便的实现。 但是在此之前,我们必须首先了解键盘按键对应的十进制ASCII码值,下面这张表是每个键盘按键对应的十进制ASCII码值表,会给我们带来很大的方便: <table border="1" cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p align="center"><span style="font-size:18px">Esc</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">27</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">#3</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">51</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">&gt;.</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">190</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">M</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">77</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">←</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">37</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F1</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">112</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">$4</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">52</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">?/</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">191</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">N</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">78</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">↑</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">38</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F2</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">113</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">%5</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">53</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Ctrl</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">17</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">O</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">79</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">→</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">39</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F3</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">114</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">^6</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">54</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">左徽标</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">91</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">P</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">80</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">↓</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">40</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F4</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">115</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">&amp;7</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">55</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">右徽标</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">92</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Q</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">81</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">Num Lock</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">144</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F5</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">116</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">*8</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">56</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Alt</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">18</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">R</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">82</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">/</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">111</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F6</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">117</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">(9</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">57</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Space</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">32</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">S</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">83</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">*</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">106</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F7</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">118</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">_-</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">189</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">鼠标右快捷键</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">93</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">T</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">84</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">-</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">109</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F8</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">119</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">+=</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">187</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">A</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">65</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">U</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">85</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">+</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">107</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F9</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">120</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Backspace</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">8</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">B</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">66</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">V</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">86</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">.</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">110</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F10</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">121</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Tab</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">9</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">C</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">67</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">W</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">87</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">0</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">96</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F11</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">122</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">{[</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">219</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">D</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">68</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">X</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">88</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">1</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">97</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">F12</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">123</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">}]</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">221</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">E</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">69</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Y</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">89</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">2</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">98</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">PrScrn SysRq</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">44</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">|\</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">220</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">F</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">70</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Z</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">90</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">3</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">99</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">Scroll Lock</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">145</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Caps Lock</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">20</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">G</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">71</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Insert</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">45</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">4</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">100</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">Pause Break</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">19</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">:;</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">186</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">H</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">72</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Home</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">36</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">5</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">101</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">~`</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">192</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">“’</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">222</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">I</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">73</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">PgUp</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">33</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">6</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">102</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">)0</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">48</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Enter</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">13</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">J</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">74</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Delete</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">46</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">7</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">103</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">!1</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">49</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">Shift</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">16</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">K</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">75</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">End</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">35</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">8</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">104</span></p></td></tr><tr><td valign="top"><p align="center"><span style="font-size:18px">@2</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">50</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">&lt;,</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">188</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">L</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">76</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">PgDn</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">34</span></p></td><td valign="top" style="background:#D9D9D9"><p align="center"><span style="font-size:18px">9</span></p></td><td valign="top"><p align="center"><span style="font-size:18px">105</span></p></td></tr></tbody></table> 注:表中的阴影部分为键盘上的数字小键盘分区。 下面结合实例来详细说明(完整实例可在我的CSDN资源中下载:[http://download.csdn.net/detail/margin1988/6648153](http://download.csdn.net/detail/margin1988/6648153)): (1)在对话框.h文件中添加函数声明: ~~~ afx_msg BOOL PreTranslateMessage(MSG* pMsg); ~~~ (2)在对话框的.cpp文件中实现该函数功能: ~~~ BOOL CPoint23Dlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->message == WM_KEYDOWN) { switch (pMsg->wParam) { case 65: MessageBox("您按了“A键”"); break; //其它按键响应 } return true;//不再响应其它按键(很重要) } return CDialog::PreTranslateMessage(pMsg); } ~~~