您不需要其他应用程序。只需创建另一种形式,这样您就可以更好地处理焦点和隐藏。我不确定你所说的“就在”你的应用程序是什么意思,但我想你的意思是窗口的位置应该在应用程序窗口的下方。看到这个sn-p:
有两种形式:MainForm 和 KeyboardForm。
unit MainFormUnit;
uses (...),KeyboardForm;
(...)
var KeybdShown: boolean = false;
procedure TMainForm.InputEditEnter(Sender: TObject); // OnEnter event
begin
if not KeybdShown then begin
KeybdShown:=true;
KeyboardForm.Top:=Top+ClientHeight;
KeyboardForm.Left:=Left;
KeyboardForm.ShowKeyboard(InputEdit); //Shows the keyboard form and sends our edit as parameter
end;
end;
procedure TMainForm.InputEditExit(Sender: TObject); // OnExit event
begin
KeyboardForm.Hide;
KeybdShown:=false;
end;
...
unit KeyboardFormUnit;
var FocusedControl: TObject;
implementation
uses MainFormUnit;
procedure TKeyboardForm.FormKeyPress(Sender: TObject; var Key: Char);
var VKRes: SmallInt;
VK: byte;
State: byte;
begin
VKRes:=VkKeyScanEx(Key, GetKeyboardLayout(0)); // Gets Virtual key-code for the Key
vk:=vkres; // The virtualkey is the lower-byte
State:=VKRes shr 8; // The state is the upper-byte
(FocusedControl as TEdit).SetFocus; // Sets focus to our edit
if (State and 1)=1 then keybd_event(VK_SHIFT,0,0,0); // These three procedures
if (State and 2)=2 then keybd_event(VK_CONTROL,0,0,0); // send special keys(Ctrl,alt,shift)
if (State and 4)=4 then keybd_event(VK_MENU,0,0,0); // if pressed
keybd_event(VK,0,0,0); // sending of the actual keyboard button
keybd_event(VK,0,2,0);
if (State and 1)=1 then keybd_event(VK_SHIFT,0,2,0);
if (State and 2)=2 then keybd_event(VK_CONTROL,0,2,0);
if (State and 4)=4 then keybd_event(VK_MENU,0,2,0);
Key:=#0;
end;
procedure TKeyboardForm.ShowKeybd(Focused: TObject);
begin
FocusedControl:=Focused;
Show;
end;
这基本上就是您处理显示/隐藏表单所需的全部内容。由于 KeyboardForm 未在启动时显示,因此它不会获得焦点(除非 Edit 将 TabOrder 设置为 0 且 TabStop 为 true - 然后 OnEnter 事件在应用程序启动时触发)。
工作原理
- 当您选择编辑时,会调用 ShowKeyboard 函数,并将编辑作为参数传递
- 显示触摸键盘,每次单击都会触发 TKeyboardForm 的 OnKeyPress 事件(!!!将 KeyPreview 设置为 true)
- 字符被解码为实际的键盘按钮(Shift、Alt、Control 和其他按钮的组合)
- 这些解码的击键被发送到编辑
注意:可以使用 SendInput() 代替 keybd_event。