Jump to content

FormKeyDown & Key: Word


cyracks

Recommended Posts

Hello,I would like to implement keyboard monitoring on a form and as I understand that is possible with option Form.KeyPreview := True and procedure FormKeyDown, for example:

procedure TMainForm.UniFormKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);
begin
end;


But I have a problem with translating variable Key to string. Code bellow works ok for numbers, but any "spacial" char is not readable (for example ": / ?"  are not recognized). I am using barcode scanner as an input.

procedure TMainForm.UniFormKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);
    Function GetCharFromVKey( vkey: word ): String;
    var
      keystate: TKeyboardState;
      retcode: Integer;
    begin
      Win32Check( GetKeyboardState( keystate ));
      SetLength( Result, 2 );
      retcode := ToAscii( vkey,
                    MapVirtualKey( vkey, 0 ),
                    keystate,
                    @Result[1],
                    0 );
      Case retcode Of
        0: Result := ''; // no character
        1: SetLength(Result,1);
        2: ;
      Else
        Result := ''; // retcode < 0 indicates a dead key
      End;
    end;

begin
    UniEdit1.Text := GetCharFromVKey(Key);
end;

Can anybody tell me what is the proper way to translate Key: Word to String ?

If I scan directly to edit field it shows correct string (and no scanning directly to edit field is not a solution I am looking for).

Regards
Tomaž

Link to comment
Share on other sites

7 hours ago, cyracks said:

I am using barcode scanner as an input.

Hello,

Your case is not very clear. Please explain in more details.

7 hours ago, cyracks said:

GetKeyboardState

You cannot use this on the client side.

You may need to use these events for your edit control:

OnKeyPress, OnChange.

Link to comment
Share on other sites

I would like to read keyboard input without edit control, but I do not know how to read variable Key which is of type Word. I tried to translate numeric value in variable Key with a function ToAscii but it does not work for "special" characters.

So question is how do I translate variable Key to the right alfanumeric character.

Link to comment
Share on other sites

6 minutes ago, cyracks said:

I would like to read keyboard input without edit control, but I do not know how to read variable Key which is of type Word. I tried to translate numeric value in variable Key with a function ToAscii but it does not work for "special" characters.

So question is how do I translate variable Key to the right alfanumeric character.

Sorry, your case is not clear to me...

Link to comment
Share on other sites

I will try to simplify. Program consists of only one empty Form and Form have declared only one event that is procedure UniFormKeyDown. So when Form is active and you enter anything on keyboard procedure UniFormKeyDown is called. Input variable is Key which is of type Word. So if you press letter A variable Key does not hold actual letter but numeric representation that is number 65. I would like to know how to convert numeric representation of written character to actual alfanumeric character, so how to convert 65 to letter A. 

What is the correct way to convert numeric variable Key of type Word to actual String (by default UTF8).

Link to comment
Share on other sites

Sadly the proposed solution does not work as expected. Event UniFormKeyPress is not executed everytime when key is pressed. For example when UniButton have a focus (if it is clicked) procedure UniFormKeyPress is not executed, but UniFormKeyDown is executed despite UniButton having focus.

Any other idea is more then welcome.

 

Link to comment
Share on other sites

I hope I figure it out, by using both events. In event UniFormKeyPress char is saved to a variable, but in procedure UniFormKeyDown action is executed when enter is pressed.

The problem is that when some components (Button) are focused enter is NOT passed to procedure UniFormKeyPress but it is to UniFormKeyDown (probably because that would trigger click on the button).

The solution for my scanning app is:

// CREATE STRING FROM INPUT
procedure TMainForm.UniFormKeyPress(Sender: TObject; var Key: Char);
begin
	if Key <> #13 then begin
		ucScann := ucScann+Key;
	end;
end;
// EXECUTE STRING ON ENTER
procedure TMainForm.UniFormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
    if Key = 13 then begin
		executeString(ucScann);
    end;
end;

 

Link to comment
Share on other sites

On 8/10/2021 at 1:57 PM, cyracks said:

I will try to simplify. Program consists of only one empty Form and Form have declared only one event that is procedure UniFormKeyDown. So when Form is active and you enter anything on keyboard procedure UniFormKeyDown is called. Input variable is Key which is of type Word. So if you press letter A variable Key does not hold actual letter but numeric representation that is number 65. I would like to know how to convert numeric representation of written character to actual alfanumeric character, so how to convert 65 to letter A. 

What is the correct way to convert numeric variable Key of type Word to actual String (by default UTF8).

Hello. CHR function doesn't work for your needs ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...