防止TouchKeyboard抓住焦点

时间:2011-05-23 05:05:34

标签: delphi keyboard focus virtual

我在Delphi中为平板电脑编写了一个小应用程序。所以没有键盘。应用程序中有一个小表单,用户可以在其中输入驱动程序名称。我想在表单上放置一个TouchKeyboard,但由于表单本身很小,因此无法容纳虚拟键盘。我可以使键盘的尺寸变小,但在这种情况下,键入起来非常困难。所以我决定编写另一个仅由键盘组成的应用程序。当主应用程序中的DBEdit聚焦时,我想执行Touchkeyboard应用程序,当DBEdit失去焦点时关闭Touchkeyboard应用程序。我的一个问题是如何防止Touchkeyboard专注于发布。另一个是我如何在主应用程序下显示Touchkeyboard。提前谢谢。

3 个答案:

答案 0 :(得分:2)

您不需要其他应用。只需创建另一个表单,这样您就可以更好地处理焦点和隐藏。我不确定你的应用程序“正好在”下面是什么意思,但我想你的意思是窗口的位置应该低于应用程序窗口。请参阅以下代码段:

有两种形式: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没有显示在开始状态,因此它不需要聚焦(除非编辑将TabOrder设置为0且TabStop为true,否则OnEnter事件将随应用程序的启动而触发)。

工作原理

  • 当您选择编辑时,将调用ShowKeyboard函数,将编辑作为参数传递
  • 显示触摸键盘,每次点击它都会触发TKeyboardForm的OnKeyPress事件(!!!将KeyPreview设置为true)
  • 将字符解码为实际的键盘按钮(Shift,Alt,Control和其他按钮的组合)
  • 这些解码的击键被发送到编辑

注意:可以使用SendInput()代替keybd_event。

答案 1 :(得分:2)

有关虚拟键盘设计的非常有趣的文章和讨论,请参阅http://www.virtual-keyboard-design.com

答案 2 :(得分:1)

Responses我成功尝试提供Delphi键盘可能很有用。