按Ctrl键时移动远程访问窗口

9
我有以下问题:我有一个应用程序,其中Ctrl键会触发应用程序事件,一些用户使用RDP(远程访问)来使用该应用程序,问题在于每当用户移动RDP窗口或切换应用程序并返回到RDP时,Ctrl键就会被触发。
例如:
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_CONTROL) then
    ShowMessage('Ctrl Pressed');
end;

我发现这个应用程序能够检测到WM_KEYUP消息并进行处理,从而触发带有参数17(Ctrl)的OnKeyUp事件,模拟按下Ctrl键。

我想知道是否有人知道这种行为是Delphi/RDP中的错误,以及是否有任何可能的解决方法。

我正在使用Delphi 10 Seatle。 enter image description here


1
复现(Delphi 10.4.1. Windows 10 2004)。非常奇怪的行为。 - Brian
1
真的很奇怪,我从Linux桌面通过RDP协议使用Remmina连接到我的开发Windows 10后,每次焦点进入远程计算机时我也会收到KEY_UP,但是在我的测试中,我总是收到VK_TAB。 - Miroslav Penchev
1个回答

4

看起来Windows会发送松开按键的信号以清除修饰键状态。一个解决方案是确保在执行松开操作前有一个按下操作。

但是CTRL键也用于切换桌面(等其他功能),CTRL-Win+箭头在切换桌面时会触发对话框,所以可能需要添加更多的保护代码。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    CtrlDown : boolean;
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_CONTROL) then CtrlDown := true;
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if (Key = VK_CONTROL) and CtrlDown then
  begin
    ShowMessage('Ctrl Pressed');
    CtrlDown := false;
  end;
end;

end.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接