•
•
•
•
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
![]() |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
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:
Hope this helps.
In either case, you should not enable the timer in a timer event. Only disable.
The way to do it is:
Delphi Syntax (Toggle Plain Text)
procedure Form1.sendNsecondsWorthOfBtoNotepad( n: integer ); // Add this procedure to the private, protected, or public part of your form class. begin timer1.interval := 100; // one keypress every 1/10th second timer1.tag := n /10; // number of timeout events to process timer1.enabled := TRUE end; procedure Form1.Timer1Timer( sender: tObject ); // This is the timeout event begin // Send the keystroke keybd_event(Ord('B'), 0, 0, 0); // One tenth second has passed timer1.tag := timer1.tag -100; // If time is up, stop sending keystrokes if timer1.tag = 0 then timer1.enabled := FALSE end; procedure Form1.Button1Click( sender: tObject ); // This procedure starts the process var ms: integer; begin // Convert the user's input if possible. (If not, complain.) try ms := strToInt( edit1.text ) except showMessage( 'You must specify the number of milliseconds to send keystrokes' ); edit1.setFocus; exit end; sendNsecondsWorthOfBtoNotepad( ms ) end;
Hope this helps.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
I'm still unsure of where to add the procedure even though it says:
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.
•
•
•
•
//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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
Make sure your form class looks something like this:
Hope this helps.
Delphi Syntax (Toggle Plain Text)
type TForm1 = class(TForm) ... public: { Public declarations } procedure sendNsecondsWorthOfBtoNotepad( n: integer ); end; ... implementation {$R *.RES} procedure Form1.sendNsecondsWorthOfBtoNotepad( n: integer ); var ... begin ... end;
Hope this helps.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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:
<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:

gives me the following errors:
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.
The line:
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'
Oh, and I'm jealous of your skill in Delphi!!!!
Ab.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation:
Rep Power: 11
Solved Threads: 193
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: A Little Pascal Help
- Next Thread: push and pop missing?



Linear Mode