User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 423,576 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,399 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1478 | Replies: 6 | Solved
Reply
Join Date: Feb 2008
Posts: 5
Reputation: Aborad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aborad Aborad is offline Offline
Newbie Poster

[Delphi] Finding a window and sending keypresses!

  #1  
Feb 29th, 2008
Hi there,

I just bumped across this website through google and found it very neat. I am self taught at Delphi, and know very little. I know there are quite a few ways to send the virtual keypresses, but I only know of one and I doubt I am using it properly.


procedure TForm1.Timer1Timer(Sender: TObject);
var
Int10: Integer;
begin
if Odd(GetAsyncKeyState(VK_F10)) then //Hotkey on F10.
  begin
    Inc(Int10);
    Timer2.Interval:=StrToInt(Edit1.Text);
  end;
if odd(Int10) then //If on
  begin
    Timer2.Enabled:=True; //Enables the keypress.
  end
else //If off
  begin
    Timer2.Enabled:=False;//Disables the keypress.
  end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
var
Window:hwnd;
begin
Window:=FindWindowA('notepad',nil); //Finds notepad.
if Window<>0 then
begin
keybd_event(Ord('B'), 0, 0, 0);
end;
end;

I found Delphi Tricks and there are a few ways of sending key presses, however I do not understand them. I only want Keydown in this application so it holds down B. I have tried googling and found some other examples, but I'm not sure if I am incorporating them in properly.

I appreciate your assistance!

Sincerely,
Ab.

Post Scriptum: I apologize if I was not being specific enough.
Last edited by Aborad : Feb 29th, 2008 at 10:02 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: [Delphi] Finding a window and sending keypresses!

  #2  
Feb 29th, 2008
That timer event will not work like you expect. The Int10 is not guaranteed to have any specific value. While it may appear to work sometimes (just as a fluke of how the stack works), it will suddenly break and work differently.

In either case, you should not enable the timer in a timer event. Only disable.

The way to do it is:
  1. procedure Form1.sendNsecondsWorthOfBtoNotepad( n: integer );
  2. // Add this procedure to the private, protected, or public part of your form class.
  3. begin
  4. timer1.interval := 100; // one keypress every 1/10th second
  5. timer1.tag := n /10; // number of timeout events to process
  6. timer1.enabled := TRUE
  7. end;
  8.  
  9. procedure Form1.Timer1Timer( sender: tObject );
  10. // This is the timeout event
  11. begin
  12. // Send the keystroke
  13. keybd_event(Ord('B'), 0, 0, 0);
  14.  
  15. // One tenth second has passed
  16. timer1.tag := timer1.tag -100;
  17.  
  18. // If time is up, stop sending keystrokes
  19. if timer1.tag = 0 then timer1.enabled := FALSE
  20. end;
  21.  
  22. procedure Form1.Button1Click( sender: tObject );
  23. // This procedure starts the process
  24. var ms: integer;
  25. begin
  26. // Convert the user's input if possible. (If not, complain.)
  27. try ms := strToInt( edit1.text )
  28. except
  29. showMessage( 'You must specify the number of milliseconds to send keystrokes' );
  30. edit1.setFocus;
  31. exit
  32. end;
  33. sendNsecondsWorthOfBtoNotepad( ms )
  34. end;

Hope this helps.
Reply With Quote  
Join Date: Feb 2008
Posts: 5
Reputation: Aborad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aborad Aborad is offline Offline
Newbie Poster

Re: [Delphi] Finding a window and sending keypresses!

  #3  
Mar 1st, 2008
I'm still unsure of where to add the procedure even though it says:
//Add this procedure to the private, protected, or public part of your form class.

I tried to put it in a couple of different places (like under the public declarations), but it did not work (meaning I got an error). I know this is supposed to be obvious, but I unfortunately have little experience.
Last edited by Aborad : Mar 1st, 2008 at 4:09 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: [Delphi] Finding a window and sending keypresses!

  #4  
Mar 1st, 2008
Make sure your form class looks something like this:
  1. type
  2. TForm1 = class(TForm)
  3. ...
  4. public:
  5. { Public declarations }
  6. procedure sendNsecondsWorthOfBtoNotepad( n: integer );
  7. end;
  8.  
  9. ...
  10.  
  11. implementation
  12. {$R *.RES}
  13.  
  14. procedure Form1.sendNsecondsWorthOfBtoNotepad( n: integer );
  15. var ...
  16. begin
  17. ...
  18. end;

Hope this helps.
Reply With Quote  
Join Date: Feb 2008
Posts: 5
Reputation: Aborad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aborad Aborad is offline Offline
Newbie Poster

Re: [Delphi] Finding a window and sending keypresses!

  #5  
Mar 3rd, 2008
Thank you so much for replying, I really don't know how to thank you. The problem seemed to be solved, but another one came up! Here is your source:

unit Unit1;<br />
<br />
interface<br />
<br />
uses<br />
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br />
  Dialogs, StdCtrls, ExtCtrls;<br />
<br />
type<br />
  TForm1 = class(TForm)<br />
    Timer1: TTimer;<br />
    Edit1: TEdit;<br />
    Button1: TButton;<br />
    procedure Timer1Timer(Sender: TObject);<br />
    procedure Button1Click(Sender: TObject);<br />
  private<br />
    { Private declarations }<br />
  public<br />
    { Public declarations }<br />
    procedure sendNsecondsWorthOfBtoNotepad( n: integer );<br />
  end;<br />
<br />
var<br />
  Form1: TForm1;<br />
<br />
implementation<br />
<br />
{$R *.dfm}<br />
<br />
procedure TForm1.sendNsecondsWorthOfBtoNotepad(n: Integer);<br />
begin  <br />
Timer1.Interval := 100;<br />
Timer1.Tag := n /10;<br />
Timer1.Enabled  := True<br />
end;<br />
<br />
procedure TForm1.Timer1Timer(Sender: TObject);<br />
begin<br />
keybd_event(Ord('B'), 0, 0, 0);<br />
Timer1.Tag := Timer1.Tag -100;<br />
if Timer1.Tag = 0 then Timer1.Enabled := False<br />
end;<br />
<br />
procedure TForm1.Button1Click(Sender: TObject);<br />
var ms: Integer;<br />
begin<br />
try ms := StrToInt(Edit1.Text)<br />
except<br />
ShowMessage( 'You must specify the number of milliseconds to send keystrokes' );<br />
Edit1.SetFocus;<br />
exit<br />
end;<br />
sendNsecondsWorthOfBtoNotepad(ms)<br />
end;<br />
end.

The line:
Timer1.Tag := n /10;
gives me the following errors:
[Error] Unit1.pas(32): Incompatible types: 'Integer' and 'Extended'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
I'm trying to fix it, but I do not understand what "extended" means.

Oh, and I'm jealous of your skill in Delphi!!!!

Ab.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: [Delphi] Finding a window and sending keypresses!

  #6  
Mar 3rd, 2008
It comes with time.

Sorry, for the error. I accidentally mixed C and Pascal. The line should read
Timer1.Tag := n div 10;

The "extended" is the IEEE 80-bit Extended Precision floating point type. Pascal converts single, double, and real to extended when performing calculations.
Reply With Quote  
Join Date: Feb 2008
Posts: 5
Reputation: Aborad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aborad Aborad is offline Offline
Newbie Poster

Re: [Delphi] Finding a window and sending keypresses!

  #7  
Mar 4th, 2008
Originally Posted by Duoas View Post
The "extended" is the IEEE 80-bit Extended Precision floating point type. Pascal converts single, double, and real to extended when performing calculations.

Ohh, I never knew that until now (Cool tid-bit).

Thanks for solving my question! =]
Last edited by Aborad : Mar 4th, 2008 at 6:40 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 7:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC