防止TouchKeyboard抢占焦点

发布于 2024-11-09 10:15:43 字数 283 浏览 0 评论 0原文

我用 Delphi 为平板电脑编写了一个小应用程序。所以没有键盘。应用程序中有一个小表单,用户可以在其中输入驱动程序名称。我想在表单上放置一个触摸键盘,但由于表单本身很小,无法容纳虚拟键盘。我可以把键盘的尺寸调小,但那样的话打字会很困难。所以我决定编写另一个仅由键盘组成的应用程序。当主应用程序中的 DBEdit 获得焦点时,我想执行 Touchkeyboard 应用程序,当 DBEdit 失去焦点时,我想关闭 Touchkeyboard 应用程序。我的问题之一是如何防止触摸键盘在启动时抢占焦点。另一个是我如何在主应用程序下显示触摸键盘。提前致谢。

I've written a little app in Delphi for a Tablet PC. So there's no keyboard. And there's a small form in the application where the user can enter driver names. I wanted to put a TouchKeyboard on the form but as the form itself is small it's not possible to accommodate the virtual keyboard. I can make the keyboard's size small but in that case it'll be very difficult to type. So I've decided to write another app that is composed of just a keyboard. When a DBEdit in the main application is focused I want to execute the Touchkeyboard application and when DBEdit loses focus shut down the Touchkeyboard app. One of my questions is how to prevent Touchkeyboard from grabbing focus on launch. And the other one is how I can display the Touchkeyboard just under the main application. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深爱成瘾 2024-11-16 10:15:43

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

有 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 不会在启动时显示,因此它不会获得焦点(除非 Edit 将 TabOrder 设置为 0 并且 TabStop 设置为 true - 然后 OnEnter 事件会在应用程序启动时触发)。

工作原理

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

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

You don't need another app. Just create another form, that way you can work with focuses and hiding better. I am not sure what you mean by "just under" your app, but I suppose you mean the position of the window should be below the application window. See this snippet:

There are 2 forms: MainForm and 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;

And this is basically all you need to handle showing/hiding the form. Since the KeyboardForm is not shown on-start, it doesn't take focus, (unless the Edit has TabOrder set to 0 with TabStop true - then the OnEnter event fires with the start of the application).

How it works

  • When you select an Edit, the ShowKeyboard function is called, passing the edit as a parameter
  • The touch keyboard is shown and with each click it fires the OnKeyPress event of TKeyboardForm (!!! set KeyPreview to true)
  • The character is decoded to actual keyboard buttons (combination of Shift, Alt, Control and other buttons)
  • These decoded keystrokes are sent to the edit

Note: SendInput() can be used instead of keybd_event.

紙鸢 2024-11-16 10:15:43

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

for very interesting articles and discussion around virtual keyboard design, see http://www.virtual-keyboard-design.com

谁把谁当真 2024-11-16 10:15:43

回复我成功尝试提供 Delphi 键盘可能会引起兴趣。

Responses to my successful attempt to provide a Delphi keyboard may be of interest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文