如何解决防止TouchKeyboard抓住焦点
| 我已经在Delphi中为Tablet PC编写了一个小应用程序。因此没有键盘。在应用程序中有一个小表格,用户可以在其中输入驱动程序名称。我想在窗体上放置一个TouchKeyboard,但是由于窗体本身很小,因此无法容纳虚拟键盘。我可以减小键盘的尺寸,但是那样的话很难打字。因此,我决定编写另一个仅由键盘组成的应用程序。当主应用程序中的DBEdit处于焦点状态时,我要执行Touchkeyboard应用程序,而当DBEdit失去焦点时,请关闭Touchkeyboard应用程序。我的问题之一是如何防止Touchkeyboard专注于启动。另一个是如何在主应用程序下方显示触摸键盘。提前致谢。解决方法
您不需要其他应用。只需创建另一种形式,您就可以处理焦点并更好地隐藏。我不确定您的应用程序位于“正下方”是什么意思,但是我想您的意思是窗口的位置应位于应用程序窗口下方。请参阅以下代码段:
有两种形式: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); // These three procedures
if (State and 2)=2 then keybd_event(VK_CONTROL,0); // send special keys(Ctrl,alt,shift)
if (State and 4)=4 then keybd_event(VK_MENU,0); // if pressed
keybd_event(VK,0); // sending of the actual keyboard button
keybd_event(VK,2,0);
if (State and 1)=1 then keybd_event(VK_SHIFT,0);
if (State and 2)=2 then keybd_event(VK_CONTROL,0);
if (State and 4)=4 then keybd_event(VK_MENU,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。
,有关非常有趣的文章和有关虚拟键盘设计的讨论,请参见http://www.virtual-keyboard-design.com
,我成功尝试提供Delphi键盘的反应可能引起人们的兴趣。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。