📄 faq78.htm
字号:
<HTML>
<HEAD>
<TITLE>Receive key events for the arrow keys or the TAB key.</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Receive key events for the arrow keys or the TAB key.
</H3>
<P>
Have you ever tried to respond to the TAB key by writing an <TT>OnKeyPress</TT> handler for a control? You
probably discovered that it doesn't work, at least not for most controls. The problem is that windows only
sends keyboard events for the TAB key if the focused control specifically asks for them. The same holds true
for the arrow keys.
</P>
<P>
So how does the operating system know that the focused control wants to receive keyboard events for the TAB
key? The OS sends the focused control a <TT>WM_GETDLGCODE</TT> message to ask the control what keys it
wants to receive. The return value from this message determines what keystrokes will be sent to the control.
The return value for <TT>WM_GETDLGCODE</TT> is a combination of these values:
</P>
<UL>
<LI> <TT>DLGC_BUTTON</TT>
<LI> <TT>DLGC_DEFPUSHBUTTON</TT>
<LI> <TT>DLGC_HASSETSEL</TT>
<LI> <TT>DLGC_RADIOBUTTON</TT>
<LI> <TT>DLGC_STATIC</TT>
<LI> <TT>DLGC_UNDEFPUSHBUTTON</TT>
<LI> <TT>DLGC_WANTALLKEYS</TT>
<LI> <TT>DLGC_WANTARROWS</TT>
<LI> <TT>DLGC_WANTCHARS</TT>
<LI> <TT>DLGC_WANTMESSAGE</TT>
<LI> <TT>DLGC_WANTTAB</TT>
</UL>
<P>
To receive key events for the TAB key, the focused cotnrol must return <TT>DLGC_WANTTAB</TT>. To receive
key events for the arrow keys, return <TT>DLGC_WANTARROWS</TT>. If you want to receive events for both the
arrow keys and the TAB key, then bitwise OR <TT>DLGC_WANTARROWS</TT> with <TT>DLGC_WANTTAB</TT> and return
that value. If you want to receive key events for all keys, then return <TT>DLGC_WANTALLKEYS</TT>.
</P>
<P>
The VCL contains some good examples of how to use the <TT>WM_GETDLGCODE</TT> message. <TT>TToolBar</TT>
responds to <TT>WM_GETDLGCODE</TT> so it can receive events for the arrow keys, and <TT>TCustomGrid</TT>
uses <TT>WM_GETDLGCODE</TT> to request both the arrow keys and the TAB key. <TT>TMediaPlayer</TT> and
<TT>TDBNavigator</TT> use <TT>WM_GETDLGCODE</TT> to request the arrow keys. Here are some code snippets
from the VCL that demonstrate how they respond to the <TT>WM_GETDLGCODE</TT> message. I have converted code
from pascal to C++ for readability.
</P>
<pre>
<b>void</b> <b>__fastcall</b> TToolBar<b>:</b><b>:</b>WMGetDlgCode<b>(</b>TMessage <b>&</b> Message<b>)</b>
<b>{</b>
<font color="navy">// only want the arrow keys in some situations</font>
<b>if</b><b>(</b>FInMenuLoop<b>)</b>
Message<b>.</b>Result <b>=</b> DLGC_WANTARROWS<b>;</b>
<b>}</b>
<b>void</b> <b>__fastcall</b> TCustomGrid<b>:</b><b>:</b>WMGetDlgCode<b>(</b>TMessage <b>&</b> Message<b>)</b>
<b>{</b>
<font color="navy">// always want the arrow keys</font>
Msg<b>.</b>Result <b>:</b><b>=</b> DLGC_WANTARROWS<b>;</b>
<b>...</b>
<font color="navy">// sometimes want the tab key</font>
<b>if</b> <b>(</b>Options<b>.</b>Contains<b>(</b>goTabs<b>)</b><b>)</b>
Msg<b>.</b>Result <b>|=</b> DLGC_WANTTAB<b>;</b>
<font color="navy">// sometimes want WM_CHAR messages</font>
<b>if</b> <b>(</b>Options<b>.</b>Contains<b>(</b>goEditing<b>)</b><b>)</b>
Msg<b>.</b>Result <b>|=</b> DLGC_WANTCHARS<b>;</b>
<b>}</b>
<b>void</b> <b>__fastcall</b> TMediaPlayer<b>:</b><b>:</b>WMGetDlgCode<b>(</b>TMessage <b>&</b>Message<b>)</b>
<b>{</b>
Message<b>.</b>Result <b>=</b> DLGC_WANTARROWS<b>;</b>
<b>}</b>
</pre>
<P>
<B>Note:</B> Don't create a <TT>WM_GETDLGCODE</TT> handler for your form. You must
write the <TT>WM_GETDLGCODE</TT> handler at the control level.
</P>
<P>
<B>Note:</B> In VCL programs, the operating system is not actually the creator of
the <TT>WM_GETDLGCODE</TT> message. The VCL synthesizes the message. Fortunately,
this is an implementation detail that you can ignore. Simply write your
<TT>WM_GETDLGCODE</TT> handler as if the OS was the creator of the message.
</P>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -