⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq77.htm

📁 C++builder学习资料C++builder
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>

<HEAD>

   <TITLE>Send keystrokes to a control or window</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>

<H3>

Send keystrokes to a control or window

</H3>

<P>

The API function <TT>keybd_event</TT> allows you to synthesize a keyboard event. This is the same function that the

keyboard driver uses to tell the OS that the user is pressing or releasing a key. The keyboard messages will be sent

to the control or window that currently has the input focus. To simulate a key press, you call <TT>keybd_event</TT> once

to simulate the user pressing a key, and then you call <TT>keybd_event</TT> a second time to signal that the key was

released.

</P>

<UL>

<LI><A HREF="#keybd_event">Understanding the keybd_event function</A>

<LI><A HREF="#example"    >Sample code</A>

<LI><A HREF="#control"    >Encapsulating keybd_event</A>

<LI><A HREF="#notes"      >Notes</A>

</UL>





<BR>

<H4>

<A NAME="keybd_event">Understanding the keybd_event function</A>

</H4>

<P>

The prototype for <TT>keybd_event</TT> looks like this:

</P>

<pre>

VOID keybd_event<b>(</b>  BYTE bVk<b>,</b>         <font color="navy">// virtual-key code</font>

                   BYTE bScan<b>,</b>       <font color="navy">// hardware scan code</font>

                   DWORD dwFlags<b>,</b>    <font color="navy">// options flag</font>

                   DWORD dwExtraInfo <font color="navy">// additional data</font>

                <b>)</b><b>;</b>

</pre>

<P>

The first argument is the virtual key code for the key that you want to simulate. Possible values include

<TT>VK_RETURN</TT>, <TT>VK_NUMPAD0</TT>, <TT>VK_F1</TT>, and the ASCII codes for the upper case alpha keys and

the numeric keys. For a complete list of virtual key codes, look in the API include file <TT>WINUSER.H</TT>, or look in the microsoft API help file

win32.hlp under the index Virtual-Key Codes.

</P>

<P>

The second argument to <TT>keybd_event</TT> specifies the hardware scan code of the key. You can usually set this value

to 0 when you specify the virtual key code in the first argument. Hardware scan codes are OEM dependent, and the only

way to reliably obtain one is to pass a virtual key code to the <TT>MapVirtualKey</TT> API funtion.

</P>

<P>

The <TT>dwFlags</TT> argument is a bit flag that be can contain any or none of the following values.

</P>

<PRE>

    KEYEVENTF_EXTENDEDKEY

    KEYEVENTF_KEYUP

</PRE>

<P>

The first flag works in conjunction with the scan code, so you won't use it very often. The <TT>KEYEVENTF_KEYUP</TT>

flag tells the OS that the key is being released.

</P>

<P>

The <TT>dwExtraInfo</TT> parameter is a utility argument that is rarely used.

</P>

<BR>

<H4>

<A NAME="example">Sample code</A>

</H4>

<P>

The examples below demonstrate how to use <TT>keybd_event</TT> to simulate a variety of keystrokes.

</P>

<pre>

<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button1Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

<b>{</b>

    <font color="navy">// This function simulates the press of a lower case 'a'</font>

    <font color="navy">// The key will be 'a' and not 'A' because we don't</font>

    <font color="navy">// simulate a press of the shift key. Note that we</font>

    <font color="navy">// must pass upper case letters to keybd_event.</font>



    <font color="navy">// Note: if you hold down the shift key while testing this code</font>

    <font color="navy">//       an uppercase 'A' will appear in the edit box</font>



    <font color="navy">// Focus the edit control so it receives the key event</font>

    Edit1<b>-></b>SetFocus<b>(</b><b>)</b><b>;</b>



    <font color="navy">// Simulate the user pressing the A on the keyboard</font>

    keybd_event<b>(</b><font color="blue">'A'</font><b>,</b> <font color="blue">0</font><b>,</b><font color="blue">0</font><b>,</b><font color="blue">0</font><b>)</b><b>;</b>



    <font color="navy">// Simulate the user releasing the the A on the keyboard</font>

    keybd_event<b>(</b><font color="blue">'A'</font><b>,</b> <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

<b>}</b>





<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Button2Click<b>(</b>TObject <b>*</b>Sender<b>)</b>

<b>{</b>

    <font color="navy">// The previous function put an 'a' in the edit box if the code</font>

    <font color="navy">// happened to run while the user was not holding the shift key or had</font>

    <font color="navy">// the caps lock on. This code ensures that a lower case 'a' is sent, even</font>

    <font color="navy">// if the user is holding down the ctrl key, alt key, the shift key, etc</font>



    <font color="navy">// Find out which of the special keys are down</font>

    <b>bool</b> bCtrlDown     <b>=</b> GetKeyState <b>(</b>VK_CONTROL<b>)</b> <b>&</b>  <font color="blue">0x8000</font><b>;</b>

    <b>bool</b> bAltDown      <b>=</b> GetKeyState <b>(</b>VK_MENU<b>)</b>    <b>&</b>  <font color="blue">0x8000</font><b>;</b>

    <b>bool</b> bShiftDown    <b>=</b> GetKeyState <b>(</b>VK_SHIFT<b>)</b>   <b>&</b>  <font color="blue">0x8000</font><b>;</b>

    <b>bool</b> bLWindowsDown <b>=</b> GetKeyState <b>(</b>VK_LWIN<b>)</b>    <b>&</b>  <font color="blue">0x8000</font><b>;</b>

    <b>bool</b> bRWindowsDown <b>=</b> GetKeyState <b>(</b>VK_RWIN<b>)</b>    <b>&</b>  <font color="blue">0x8000</font><b>;</b>

    <b>bool</b> bAppsDown     <b>=</b> GetKeyState <b>(</b>VK_APPS<b>)</b>    <b>&</b>  <font color="blue">0x8000</font><b>;</b>

    <b>bool</b> bCapsOn       <b>=</b> GetKeyState <b>(</b>VK_CAPITAL<b>)</b> <b>&</b>  <font color="blue">0x0001</font><b>;</b>



    <font color="navy">// For each special key that was down, simulate that the</font>

    <font color="navy">// user has let go of the key</font>

    <b>if</b><b>(</b>bCtrlDown<b>)</b>      keybd_event<b>(</b>VK_CONTROL<b>,</b> <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

    <b>if</b><b>(</b>bAltDown<b>)</b>       keybd_event<b>(</b>VK_MENU<b>,</b>    <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

    <b>if</b><b>(</b>bShiftDown<b>)</b>     keybd_event<b>(</b>VK_SHIFT<b>,</b>   <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

    <b>if</b><b>(</b>bLWindowsDown<b>)</b>  keybd_event<b>(</b>VK_LWIN<b>,</b>    <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

    <b>if</b><b>(</b>bRWindowsDown<b>)</b>  keybd_event<b>(</b>VK_RWIN<b>,</b>    <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

    <b>if</b><b>(</b>bAppsDown<b>)</b>      keybd_event<b>(</b>VK_APPS<b>,</b>    <font color="blue">0</font><b>,</b>KEYEVENTF_KEYUP<b>,</b><font color="blue">0</font><b>)</b><b>;</b>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -