Solved

Reading keyboard buffer

Posted on 2002-04-21
11
1,869 Views
Last Modified: 2007-11-27
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
Comment
Question by:miket6000
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 6959008
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
 
LVL 1

Expert Comment

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

WBR...
0
 
LVL 2

Expert Comment

by:Kalroth
ID: 6961022
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Expert Comment

by:lottol
ID: 6961858
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
 

Author Comment

by:miket6000
ID: 6964733
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
 
LVL 7

Expert Comment

by:Cynna
ID: 7062619
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
 

Author Comment

by:miket6000
ID: 7066174
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
 

Expert Comment

by:lottol
ID: 7066310
You can put a keyboards hook onto interested window or all windows
0
 
LVL 7

Accepted Solution

by:
Cynna earned 100 total points
ID: 7066454
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
 

Author Comment

by:miket6000
ID: 7069095
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
 

Author Comment

by:miket6000
ID: 7069107
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now