Lazarus

Free Pascal => General => Topic started by: TheLastCayen on March 30, 2021, 05:58:27 am

Title: Virtual Key output to external software
Post by: TheLastCayen on March 30, 2021, 05:58:27 am
Hi,

I am using
 - Linux mint 20.1 XFCE 64 bit
 - Lazarus 2.0.12
 - FPC 3.2.0
 
I did a small software to type my password for me. My goal is to type the text when I push F5. I have no problem with the F5 part, tested with ShowMessage('Hellow World'); .  My problem is if I open a text editor and I push F5, it won't output my password(But again it will show "hello world" no problem). If I am in my application, click on an edit filed and push F5, It will enter my password without the special character.... I have 2 questions:

  - How Can I output text in an external application not managed per my software?
  - How Can I output Special Character. 

This is my code:
Code: Pascal  [Select][+][-]
  1. procedure TFMain.KeyPressed(Sender: TObject; Key: Word; Shift: TShiftState);
  2. var
  3.   I: integer;
  4. begin
  5.   if Key = Fkey then
  6.     begin
  7.       p:=MEPassword.Text;
  8.       For I := 1 to Length(P) do
  9.         begin
  10.           if IsUpper(P[I]) then KeyInput.Apply([ssShift]);
  11.           KeyInput.Press(Char2VK(P[I]));
  12.           if IsUpper(P[I]) then KeyInput.Unapply([ssShift]);
  13.         end;
  14.       P:='';
  15.       ShowMessage('Hellow World');
  16.     end;
  17. end;    
  18.  

Thank you
Title: Re: Virtual Key output to external software
Post by: MarkMLl on March 30, 2021, 08:50:05 am
I am using
 - Linux mint 20.1 XFCE 64 bit
 - Lazarus 2.0.12
 - FPC 3.2.0

Thank $DEITY that for once somebody is sufficiently clued up to tell us the important bit!

Quote
  - How Can I output text in an external application not managed per my software?

Depends how the child program is invoked. You could pipe it, or you could a pseudoterminal via ptmx. Or you could research how people write "wedges" to inject barcodes etc. (there was discussion of that in this forum a few weeks ago). BUT

Quote
  - How Can I output Special Character. 

I think the only way to do this is to rewrite entries on the event queue. See https://wiki.lazarus.freepascal.org/Lazarus_IDE_with_Nostromo_Keypad_on_Linux which is for a gamepad but might help.

MarkMLl
Title: Re: Virtual Key output to external software
Post by: TheLastCayen on March 31, 2021, 03:33:20 am
Thank you for your reply MArkMLI

For now, I will skip the part where I try to inject text into another application. I will read about what you said later. I manage to use KeyInput.Press to write basic char. But I have a hard time with a few special characters. So this is my new code:

Code: Pascal  [Select][+][-]
  1. procedure TFMain.Typetext;
  2. var
  3.   I: integer;
  4.   s: string;
  5. begin
  6.   //s:=fdecrypt(P);
  7.   s:='ABCdefGHIjklMNOpqrSTUvwxYZ*/+.-#$%^&@[\<>';  
  8.   For I:= 1 to Length(S) do
  9.     Begin
  10.       case S[I] of
  11.  
  12.         //No idea how to make those char
  13.         ':': Useshift(VK_OEM_1);
  14.         ';': KeyInput.Press(VK_OEM_1);
  15.         '>': Useshift(VK_OEM_102);
  16.         '<': KeyInput.Press(VK_OEM_102);
  17.         '?': Useshift(VK_OEM_2);
  18.        '~': Useshift(VK_OEM_3);
  19.         '`': KeyInput.Press(VK_OEM_3);
  20.         '{': Useshift(VK_OEM_4);
  21.         '[': KeyInput.Press(VK_OEM_4);
  22.         '|': Useshift(VK_OEM_5);
  23.         '\': KeyInput.Press(VK_OEM_5);
  24.         '}': Useshift(VK_OEM_6);
  25.         ']': KeyInput.Press(VK_OEM_6);
  26.         '"': Useshift(VK_OEM_7);
  27.         '''': KeyInput.Press(VK_OEM_7);
  28.         ',': Useshift(VK_OEM_COMMA);
  29.         '!': Useshift(VK_OEM_8);
  30.         '@': KeyInput.Press($40);
  31.         '#': KeyInput.Press($23);
  32.         '$': KeyInput.Press(36);
  33.         '%': KeyInput.Press(37);
  34.         '^': KeyInput.Press(94);
  35.         '&': KeyInput.Press(38);
  36.         '(': KeyInput.Press(40);
  37.         ')': KeyInput.Press(41);
  38.  
  39.         //Next Part is tested and working
  40.         '*': KeyInput.Press(VK_MULTIPLY);
  41.         '/': KeyInput.Press(VK_DIVIDE);
  42.         '+': KeyInput.Press(VK_ADD);
  43.         '.': KeyInput.Press(VK_DECIMAL);
  44.         '-': KeyInput.Press(VK_SUBTRACT);
  45.         '0': KeyInput.Press(VK_NUMPAD0);
  46.         '1': KeyInput.Press(VK_NUMPAD1);
  47.         '2': KeyInput.Press(VK_NUMPAD2);
  48.         '3': KeyInput.Press(VK_NUMPAD3);
  49.         '4': KeyInput.Press(VK_NUMPAD4);
  50.         '5': KeyInput.Press(VK_NUMPAD5);
  51.         '6': KeyInput.Press(VK_NUMPAD6);
  52.         '7': KeyInput.Press(VK_NUMPAD7);
  53.         '8': KeyInput.Press(VK_NUMPAD8);
  54.         '9': KeyInput.Press(VK_NUMPAD9);
  55.         'a': KeyInput.Press(VK_A);
  56.         'A': Useshift(VK_A);
  57.         'b': KeyInput.Press(VK_B);
  58.         'B': Useshift(VK_B);
  59.         'c': KeyInput.Press(VK_C);
  60.         'C': Useshift(VK_C);
  61.         'd': KeyInput.Press(VK_D);
  62.         'D': Useshift(VK_D);
  63.         'e': KeyInput.Press(VK_E);
  64.         'E': Useshift(VK_E);
  65.         'f': KeyInput.Press(VK_F);
  66.         'F': Useshift(VK_F);
  67.         'g': KeyInput.Press(VK_G);
  68.         'G': Useshift(VK_G);
  69.         'h': KeyInput.Press(VK_H);
  70.         'H': Useshift(VK_H);
  71.         'i': KeyInput.Press(VK_I);
  72.         'I': Useshift(VK_I);
  73.         'j': KeyInput.Press(VK_J);
  74.         'J': Useshift(VK_J);
  75.         'k': KeyInput.Press(VK_K);
  76.         'K': Useshift(VK_K);
  77.         'l': KeyInput.Press(VK_L);
  78.         'L': Useshift(VK_L);
  79.         'm': KeyInput.Press(VK_M);
  80.         'M': Useshift(VK_M);
  81.         'n': KeyInput.Press(VK_N);
  82.         'N': Useshift(VK_N);
  83.         'o': KeyInput.Press(VK_O);
  84.         'O': Useshift(VK_O);
  85.         'p': KeyInput.Press(VK_P);
  86.         'P': Useshift(VK_P);
  87.         'q': KeyInput.Press(VK_Q);
  88.         'Q': Useshift(VK_Q);
  89.         'r': KeyInput.Press(VK_R);
  90.         'R': Useshift(VK_R);
  91.         's': KeyInput.Press(VK_S);
  92.         'S': Useshift(VK_S);
  93.         't': KeyInput.Press(VK_T);
  94.         'T': Useshift(VK_T);
  95.         'u': KeyInput.Press(VK_U);
  96.         'U': Useshift(VK_U);
  97.         'v': KeyInput.Press(VK_V);
  98.         'V': Useshift(VK_V);
  99.         'w': KeyInput.Press(VK_W);
  100.         'W': Useshift(VK_W);
  101.         'x': KeyInput.Press(VK_X);
  102.         'X': Useshift(VK_X);
  103.         'y': KeyInput.Press(VK_Y);
  104.         'Y': Useshift(VK_Y);
  105.         'z': KeyInput.Press(VK_Z);
  106.         'Z': Useshift(VK_Z);
  107.       end;
  108.     end;
  109. end;
  110.  
  111. Procedure TFMain.Useshift(s:Word);
  112. begin
  113.   KeyInput.Apply([ssShift]);
  114.   KeyInput.Press(s);
  115.   KeyInput.Unapply([ssShift]);
  116. end;    
  117.  

As noted in the example, there are a few special characters I can't do. I try using the Ascii table hex and decimal... Use the code from LCLType... nothing seems to work.
Anyone knows how to use KeyInput.Press to make those characters? or an alternative?

Thank you

Title: Re: Virtual Key output to external software
Post by: MarkMLl on March 31, 2021, 09:32:44 am
I'd suggest starting off with the example program that comes with KeyInput in the FPC sources, and trying to modify that for the "specials". I notice that the only time that I've got deeply into this (writing a terminal emulator) I was using the VK_LCL codes, not VK_OEM.

Code: Pascal  [Select][+][-]
  1. (* Return true if the key is one of the standard alphanumerics, tab, space,     *)
  2. (* backspace, escape, enter (or one of the shifted variants of enter which      *)
  3. (* might represent LF or attention). In short it's not a function or cursor-    *)
  4. (* control key, or noise from a shift key                                       *)
  5. (*                                                                              *)
  6. (* Expect this to be widget-set specific, and also to vary with localisation.   *)
  7. //
  8. function typewriterKey(key: word): boolean;
  9.  
  10. begin
  11.   result := true;
  12. //  WrDebug(DbgKey, Key, 2, true);        (* Normally disabled                    *)
  13.  
  14. (* Refer to lcl/lcltype.pp                                                      *)
  15.  
  16.   case key of
  17.     VK_BACK, VK_TAB, VK_RETURN, VK_ESCAPE, VK_SPACE,
  18.     VK_0..VK_9,
  19.     VK_A..VK_Z,
  20.     VK_LCL_EQUAL, VK_LCL_COMMA, VK_LCL_POINT,
  21.     VK_LCL_SLASH, VK_LCL_SEMI_COMMA, VK_LCL_MINUS,
  22.     VK_LCL_OPEN_BRAKET, VK_LCL_CLOSE_BRAKET,
  23.     VK_LCL_BACKSLASH, VK_LCL_TILDE, VK_LCL_QUOTE,
  24.     VK_SEPARATOR, VK_SUBTRACT, VK_DECIMAL, VK_DIVIDE, (* Required for Qt as of LCL 1.0 *)
  25.  
  26. (* Non-standard keys as of Lazarus 1.0 and FPC 2.6.                             *)
  27.  
  28.     $92,                                (* Hash (unshifted) and tilde (shifted) *)
  29.     $93..$96: begin end                 (* "OEM specific"                       *)
  30.   otherwise
  31.     result := false
  32.   end
  33. end { typewriterKey } ;
  34.  

I haven't a clue what the difference is, but some of those correspond to the characters giving you problems.

(In the context of that program, I was piping characters or sending them via a socket rather than using KeyInput.Press() etc.)

MarkMLl
TinyPortal © 2005-2018