📄 keyboard.htm
字号:
{ simulate keypress sequence }
SimKeyPresses(KeyCode);
end;
end;
inc(index);
until index > Length(S);
end;
function SendKeys(S: String): TSendKeyError; export;
{ This is the one entry point. Based on the string passed in the S }
{ parameter, this function creates a list of keyup/keydown messages, }
{ sets a JournalPlayback hook, and replays the keystroke messages. }
var
i: byte;
begin
try
Result := sk_None; { assume success }
MessageList := TMessageList.Create; { create list of messages }
ProcessKey(S); { create messages from string
}
StartPlayback; { set hook and play back messages }
except
{ if an exception occurs, return an error code, and clean up }
on E:ESendKeyError do begin
MessageList.Free;
if E is ESetHookError then
Result := sk_FailSetHook
else if E is EInvalidToken then
Result := sk_InvalidToken;
end
else
{ Catch-all exception handler ensures than an exception }
{ doesn't walk up into application stack }
Result := sk_UnknownError;
end;
end;
exports
SendKeys index 1;
begin
end</PRE><HR>
<P><H1><A NAME="keyboard4">Simulating ButtonDown</P></A></H1>
<P><I>From: "James D. Rofkar" <jim_rofkar%lotusnotes1@instinet.com></I></P>
<TT>Paulo Oliveira wrote:
>
> I have a set of buttons,(caption ='0'..'9') and I'd like to simulate the down position of the button, when the user presses the corresponding key.
I.e. if user presses key '1' the button goes down on screen. How can I do this, without a new Tbutton component?
</TT>
<P>No problem Paulo:</P>
<P>You'll probably want to be using 10 TSpeedButton controls, or an array of them, since this button provides a "Down" property.
Anyhow, set the "KeyPreview" property of your main form to "True". Then, in your "OnKeyDown" event handler, write something like this...</P>
<HR><PRE> case Key of
VK_NUMPAD0: btn0.Down := True;
VK_NUMPAD1: btn1.Down := True;
VK_NUMPAD2: btn2.Down := True;
VK_NUMPAD3: btn3.Down := True;
VK_NUMPAD4: btn4.Down := True;
VK_NUMPAD5: btn5.Down := True;
VK_NUMPAD6: btn6.Down := True;
VK_NUMPAD7: btn7.Down := True;
VK_NUMPAD8: btn8.Down := True;
VK_NUMPAD9: btn9.Down := True;
end;
</PRE><HR>
<P>And, in your "OnKeyUp" event handler, write something like...</P>
<HR><PRE> case Key of
VK_NUMPAD0: btn0.Down := False;
VK_NUMPAD1: btn1.Down := False;
VK_NUMPAD2: btn2.Down := False;
VK_NUMPAD3: btn3.Down := False;
VK_NUMPAD4: btn4.Down := False;
VK_NUMPAD5: btn5.Down := False;
VK_NUMPAD6: btn6.Down := False;
VK_NUMPAD7: btn7.Down := False;
VK_NUMPAD8: btn8.Down := False;
VK_NUMPAD9: btn9.Down := False;
end;
</PRE><HR>
<P>You'll want to experiment with the "AllowAllUp" property and the "GroupIndex" property to get the button response/effect you like.</P>
<P>Again, an array of TSpeedButtons would be the most elegant solution to this problem, since you could use the VK_ constant as the index,
and make both event handlers a one line call to Button[VK_x].Down := True {or False}.</P>
<P><H1><A NAME="keyboard5">How? ENTER key instead of TAB</P></A></H1>
Here is something I picked up off Compuserve that should help. <p>
<P><I>Simon Callcott CIS: 100574,1034</I></P>
<U>Using the &tl;Enter≷ key like a &tl;Tab≷ key with Delphi Controls</U><p>
The example code supplied here demonstrates how to trap the
&tl;Enter≷ key and the cursor keys to provide better data entry
processing.<P>
The trick is to overide the Keypress and KeyDown events so
that they process the keys the way you want. In the examples
supplied I have used the &tl;Enter≷ key to move to the next
control (like the &tl;Tab≷ key) and the cursor Up and Down keys
to move to the previous and next controls respectively.<P>
The Edit and EBEdit use the cursor keys as stated above, but
the Combobox and the Listbox use Shift-Up and Shift-Down
instead so as not to interfere with existing functionality.<p>
The Grid control uses the &tl;Enter≷ key to move between fields,
however it will not move from the last field of the last row.
It is very easy to make it exit the grid at this point if you
need to.<P>
The method used to move to the next/previous control is the
Windows API call SendMessage which is used to dispatch a
WM_NEXTDLGCTL to the form the controls are children to.
Delphi provides a function called GetParentForm to get the
handle of the parent form of the control.<P>
These simple extensions can be expanded to respond to almost
any keyboard event, and I think using this method is less
trouble than trapping keys in the forms OnKey events (using
keypreview:=true).<P>
Feel free to use the code as you wish, but if you discover
something new please let me in on it!<P>
<HR><PRE>{
Edit control that reponds as if the &tl;Tab≷ key has been pressed when an
&tl;Enter≷ key is pressed, moving to the next control.
Very simple extension to the KeyPress event, this technique should work
with TDBedit as well, Useful for data entry type apps.
Less trouble than using the Keypreview function of the form to do the same
thing.
Please Use Freely.
Simon Callcott CIS: 100574, 1034
}
unit Entedit;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TEnterEdit = class(TEdit)
private
protected
procedure KeyPress(var Key: Char); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
public
published
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TEnterEdit]);
end;
procedure TEnterEdit.KeyPress(var Key: Char);
var
MYForm: TForm;
begin
if Key = #13 then
begin
MYForm := GetParentForm( Self );
if not (MYForm = nil ) then
SendMessage(MYForm.Handle, WM_NEXTDLGCTL, 0, 0);
Key := #0;
end;
if Key &tl;≷ #0 then inherited KeyPress(Key);
end;
procedure TEnterEdit.KeyDown(var Key: Word; Shift: TShiftState);
var
MYForm: TForm;
CtlDir: Word;
begin
if (Key = VK_UP) or (Key = VK_DOWN) then
begin
MYForm := GetParentForm( Self );
if Key = VK_UP then CtlDir := 1
else CtlDir :=0;
if not (MYForm = nil ) then
SendMessage(MYForm.Handle, WM_NEXTDLGCTL, CtlDir, 0);
end
else inherited KeyDown(Key, Shift);
end;
end.
</PRE><HR>
<H2>Solution 2 </H2>
Q. "Is there a way to use the return key for data entry, instead of tab or the
mouse?"<p>
<P><I>Ken Hale khale@oro.net Compuserve: 74633.2474 </I></P>
A. Use this code for an Edit's OnKeyPress event.
<HR><PRE> procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
If Key = #13 Then
Begin
SelectNext(Sender as tWinControl, True, True );
Key := #0;
end;
end;
</PRE><HR>
This causes Enter to behave like tab. Now, select all controls on the form
you'd like to exhibit this behavior (not Buttons) and go to the Object
Inspector and set their OnKeyPress handler to EditKeyPress. Now, each
control you selected will process Enter as Tab. If you'd like to handle
this at the form (as opposed to control) level, reset all the controls
OnKeyPress properties to blank, and set the _form_'s OnKeyPress property to
EditKeyPress. Then, change Sender to ActiveControl and set the form's
KeyPreview property to true: <P>
<HR><PRE> procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
If Key = #13 Then
begin
SelectNext(ActiveControl as tWinControl, True, True );
Key := #0;
end;
end;
</PRE><HR>
This will cause each control on the form (that can) to process Enter as Tab.
<!---------------------------------------------------------------------------------------------------------------------------------------------------->
<P><H1><A NAME="keyboard6">Caps Lock (and others like it)<IMG SRC="../images/new.gif" WIDTH=28 HEIGHT=11 BORDER=0 ALT=" [NEW]"></P></A></H1>
<P><I>From: m.a.vaughan@larc.nasa.gov (Mark Vaughan)</I></P>
<PRE>]-How Do I turn them on? (IN A DELPHI PROGRAM OF COURSE) i have tried and asked around</PRE>
try this...
<HR><PRE>procedure TMyForm.Button1Click(Sender: TObject);
Var
KeyState : TKeyboardState;
begin
GetKeyboardState(KeyState);
if (KeyState[VK_NUMLOCK] = 0) then
KeyState[VK_NUMLOCK] := 1
else
KeyState[VK_NUMLOCK] := 0;
SetKeyboardState(KeyState);
end;
</PRE><HR>
for caps lock substitute VK_CAPITAL for VK_NUMLOCK.
<HR SIZE="6" color="#00FF00">
<FONT SIZE="2">
<a href="mailto:rdb@ktibv.nl">Please email me</a> and tell me if you liked this page.<BR>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("Last modified " + document.lastModified);
// -->
</SCRIPT><P>
<TABLE BORDER=0 ALIGN="CENTER">
<TR>
<TD>This page has been created with </TD>
<TD> <A HREF="http://www.dexnet.com./homesite.html"><IMG SRC="../images/hs25ani.gif" WIDTH=88 HEIGHT=31 BORDER=0 ALT="HomeSite 2.5b">
</A></TD>
</TR>
</TABLE>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -