delphi - 防止TouchKeyboard抢占焦点

标签 delphi keyboard focus virtual

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

最佳答案

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

有 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。

关于delphi - 防止TouchKeyboard抢占焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6093046/

相关文章:

delphi - Delphi 的图像编辑器控件

delphi - 如何在缺失数据的不规则间隔网格或数组中填充 'holes'?

linux - 在 Linux 上使用 Python 拦截和发送击键

flutter - 为什么软件键盘会在打开/关闭时导致小部件重建?

ios - 选项卡式图标和键盘未出现在 Swift 的文本字段中

forms - 如何在 Delphi 屏幕键盘窗体中使用窗口焦点消息

jQuery focus() 位于按钮单击 ipad 上的文本框上

android - Delphi Android过滤表区分大小写

delphi - 如何在 Delphi 搜索路径中使用系统变量?

focus - 以编程方式控制网络摄像头的焦点