Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1974
  • Last Modified:

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
0
miket6000
Asked:
miket6000
  • 4
  • 2
  • 2
  • +3
1 Solution
 
viktornetCommented:
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
0
 
AloneCommented:
Or... when you're creating a console application try to use standard Read/ReadLn procedures, like pure Pascal.

WBR...
0
 
KalrothCommented:
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;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lottolCommented:
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
0
 
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
0
 
CynnaCommented:
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.
0
 
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
0
 
lottolCommented:
You can put a keyboards hook onto interested window or all windows
0
 
CynnaCommented:
miket6000,

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.

1. http://www.gajits.com/delphihooks.asp
2. http://www.q3.nu/trucomania/truco.cgi?408&ing
3. http://www.undu.com/Articles/980326c.htm
4. http://community.borland.com/article/0,1410,10323,00.html
 
0
 
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
0
 
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now