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
Who is Participating?

Improve company productivity with a Business Account.Sign Up

CynnaConnect With a Mentor Commented:

What you want is possible, but not so easy. I'll try to explain a principle, and maybe dig up some resources on the net that could help you. Complete working solution is a bit more complicated, as you'll see.

OK, firstly you can't just simply 'read' data of another processes queue. Address space of another process, including message que, is protected from anyone else. So, there is no simple way to 'peek' into anything that belongs to another process, at least if another process doesn't want you to do just that.

BUT, there is a way to 'hook' into another process; as lottol correctly suggested, you should use keyboard hook.
Hooks are way to 'force' your application inside message que of another one, or even all others. There are several types of hooks, but one that you are interested in is called (surprise, surprise...) keyboard hook. You can hook your own process (which is pretty straightforward), or another one, which requires global or system-wide hook (more difficult scenario, but this is what you need).

Writing system-wide hook includes writing a DLL and simple application that will call it to install hook. There are several 'special' API functions that are required to work with hooks, and placing them inside DLL is neccessary so that calling application could map it inside its own adress space.

There are plenty of online resouces that teach you about hooks; below are few that talk exactly about what you need. Read them, use examples, and you'll be on your way in no time.

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

        Caption := Key;

Should look like this:

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

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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);
  // #8 is backspace, I know you want to press backspace :)
  if not (Key in ['0'..'9',#8]) then Key := #0;
if you want to Handle user input in your own class (TMainForm is just ex.). You can use next construction;

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

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

Moreover see onKeyDown, onKeyUp, onKeyPress events
miket6000Author Commented:
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

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.
miket6000Author Commented:
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
miket6000Author Commented:
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
miket6000Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.