Link to home
Start Free TrialLog in
Avatar of miket6000
miket6000

asked on

Reading keyboard buffer

Hi everyone,

I would like to be able to read the keyboard buffer and get a strait character output from it. I have tried reading the information supplied in the Delphi help manual
but was unable to understand it. A code sample would be greatly apperciated

Thanks a lot, Miket6000

P.S. I am currently running Delphi 2.0 Developer if that makes any differance
Avatar of viktornet
viktornet
Flag of United States of America image

In your form's OnKeyPress event, put the following code:

        Caption := Key;

Should look like this:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
        Caption := Key;
end;


Now every time you press a key, it will be displayed in the title bar of your form.

-Vik
Avatar of Alone
Alone

Or... when you're creating a console application try to use standard Read/ReadLn procedures, like pure Pascal.

WBR...
In addition to viktornet's comment:

You may want to set the KeyPreview property of the form to receive ALL keypresses on your form.

Then use FormKeyDown to receive events when keys are pressed and FormKeyUp when they're released, this includes Shift, Ctrl and Alt through the ShiftStates.

If you want an Edit field only to receive numerical input then some simple code like this would do:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  // #8 is backspace, I know you want to press backspace :)
  if not (Key in ['0'..'9',#8]) then Key := #0;
end;
if you want to Handle user input in your own class (TMainForm is just ex.). You can use next construction;

TMainForm = class(TForm)
 private
  procedure WMKeyDown(var Message: TWMKey); message WM_KEYDOWN;
end;

procedure TMainForm.WMKeyDown(var Message: TWMKey);
begin
 if Message.CharCode = vk_Delete then
  begin
   ShowMessage('Delete pressed');
   Message.CharCode := 0; // no further processing this key
  end;
end;

Moreover see onKeyDown, onKeyUp, onKeyPress events
Avatar of miket6000

ASKER

Sorry, it seems there is a little confusion, probably on my behalf, when a key is typed it is first stored in the keyboard buffer and is then sent to the application if you are typing too fast for the computer to keep up it can just read from the buffer rather than loosing the keypresses.  This is so that what I acctually want is to be able to read keys which are stored in the buffer. This should make it possiable to read inputs which were made before the application is even started.

Thanks for all the suggestions though, Miket6000
miket6000,

Could you give an example of what you want to do in your application?
It seems to me that you don't quite understand what happens when key is pressed:

> This is so that what I acctually want is to be able to
> read keys which are stored in the buffer. This should
> make it possiable to read inputs which were made before
> the application is even started.

This is true for DOS programs, but not for Windows applications.
*Only* Win communicates "directly" with keyboard buffer.
It reads pressed keys, and then redistributes them through messages to whatever application has keyboard focus.
Win can start sending you messages only after your application is started. All previous keyboard events are placed in message que of application that was active when they happened. When your application starts it gets separate message que.
In a sense, you can take keyboard related messages in a message que as a kind of 'keyboard buffer' for your application only. So, every application has it's own 'local keyboard buffer' regulated by Win itself. Actual keypresses are obtained by Win, and then redistributed to these local keyboard buffers, depending on current keyboard foccus.
It seems that I may have been misinformed about how inputs from the keyboard are handled.

Cynna, do you know if it would be possiable to read data from a message que of another application, namely Windows 95, this should also be adequate to fulfill my needs.

If it is possiable I would greatly appreciate the help. Either way, thank you for re-educating me in the workings of the keyboard, I will award you the points if you make another post to follow up.

thanks to all who tried to help, miket6000
You can put a keyboards hook onto interested window or all windows
ASKER CERTIFIED SOLUTION
Avatar of Cynna
Cynna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cynna, thank you for all your help.
I'll read up on the resources you gave me and award you th points for your time.

Thanks again, Miket6000
Lottol, seeing as your answer was correct and I was just un aware of it, I will ask a question titled "Points" just make a comment and I will award you 100 points as well.

Thanks for all your help, miket6000