Ctrl key presses when moving remote access window
Asked Answered
C

1

10

I have the following problem: I have an application in which the Ctrl key activates an application event, and some users use RDP (remote access) to use that application, the problem is that the Ctrl key is triggered every time the user moves the RDP window or application switch and return to RDP.

For example:

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

I was able to see that the application detects the WM_KEYUP message and treats it, which ends up triggering the OnKeyUp event with parameter 17 (Ctrl), simulating that the Ctrl key was pressed.

I would like to know if anyone has any idea if this behavior is a bug in Delphi / RDP and if it has any possible solution.

I'm using Delphi 10 Seatle enter image description here

Countercheck answered 5/10, 2020 at 17:55 Comment(2)
Reproduced (Delphi 10.4.1. Windows 10 2004). Very odd behavior.Off
That's really strange, I never though there is such strange behavior. I tested with Remmina from Linux desktop to my dev Windows 10 via RDP protocol and I also get KEY_UP each time focus enters into remote machine, but on my tests I always get VK_TAB.Teador
O
4

Looks like windows sends key ups to clear the modifier key state. One solution would to be to make sure you got a down before acting on the up.

Still CTRL is also used for switching desktop (amongst other things) and CTRL-Win+Arrow will trigger the dialog when switching desktops so might need to add more guard code.

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.
Off answered 5/10, 2020 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.