Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable All Keypresses 3

Status
Not open for further replies.

abrogard

Technical User
Sep 13, 2002
58
AU

I'm writing something to enable my little baby to play with the keyboard.

This means catching the keypresses and providing an appropriate sound and picture.

Slow job.

So I want to be able to disable all keypresses that I am not currently catching, so that nothing at all happens.

Particularly so that he doesn't inadvertently throw up a menu or initiate some other programme or somesuch.

I've got two lists of keys and the code I can check for - two different 'methods' - and it is still not complete. So I'd also like, if possible, a complete list of all keys and the codes I can check for so that eventually I can complete the job and provide a handler for each and every key, upper case and lower case, even Alt+key, perhaps, though it's obviously far from necessary for a baby but it is sort of 'central' to computing and keyboard usage. Same with Ctrl+key.

Any and all help much appreciated.
 
Try with clearing keyboard's buffer.

Code:
procedure keybclear;
var
  msg: TMsg;
begin
  while PeekMessage(msg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE or PM_NOYIELD) do;
end;

if key pressed is out of the range you want, you can invoke keybclear function.


Hope this can help
Giovanni Caramia
 
Buy the baby a keyboard, 3 years from now you will have to buy him his own computer. About messing up the computer, my children / nephews / neighbors children etc. never have done anything I can't repair. I have three and they have total freedom on the computers.
Adults, on the other end, are the ones that damages computers.

Steven
 

Thanks gcaramia. I'll check it out, I imagine it will do the job fine.

What about 'escape' sequences. I'll be able to stop them?

svanels I'm not worried about him breaking the computer - I'm just trying to keep the computer sensibly responding for him while he's banging away.

Currently, with only the 26 alphabet keys handled he's able to open other windows which hang there waiting for correct input or maybe just 'enter' and he finishes up with a computer that is effectively hung up.

Before I get around to handling all the keys - IF I've got keycodes for them all - it'll be good if I can just stop all input except that which is handled. With something like an IF..ELSE loop, I suppose. Handler ELSE keybclear.

 
What about 'escape' sequences. I'll be able to stop them?"

The function i posted, clears the keyboard's buffer, so, after it's executed, it is as no key has been pressed.

post your results, please

Giovanni Caramia
 
I would use the programs onkeypress and onkeydown event handlers. The onkeydown is generally used for unusual keys (ie not A-Z and numbers). In the event handler for onkeydown you can have code like:

Code:
  if key = vk_return
    then
      showmessage('enter has been pressed.');

Onkeypress is used for the normal characters and can be used as follows:

Code:
  MessageDlg(Key + ' has been pressed', mtInformation, [mbOK], 0);
Where Key is the key that has been pressed.

As for what happens on the key presses, I would use a case statement, something like this:

Code:
  case key of
    'a' : {what happens on a};
    'b' : {what happens on b};
    'A' : {capital A};
    else 
      //what happens if not key mentioned.
      //probably put gcaramia's code for 
      //clearing buffer here.
    end;

Here is a list of key names that would be used with the onkeydown procedure:

Code:
VK_LBUTTON	Left mouse button
VK_RBUTTON	Right mouse button
VK_CANCEL	Control+Break
VK_MBUTTON	Middle mouse button
VK_BACK		Backspace key
VK_TAB		Tab key
VK_CLEAR	Clear key
VK_RETURN	Enter key
VK_SHIFT	Shift key
VK_CONTROL	Ctrl key
VK_MENU		Alt key
VK_PAUSE	Pause key
VK_CAPITAL	Caps Lock key
VK_KANA		Used with IME
VK_HANGUL	Used with IME
VK_JUNJA	Used with IME
VK_FINAL	Used with IME
VK_HANJA	Used with IME
VK_KANJI	Used with IME
VK_CONVERT	Used with IME
VK_NONCONVERT	Used with IME
VK_ACCEPT	Used with IME
VK_MODECHANGE	Used with IME
VK_ESCAPE	Esc key
VK_SPACE	Space bar
VK_PRIOR	Page Up key
VK_NEXT		Page Down key
VK_END		End key
VK_HOME		Home key
VK_LEFT		Left Arrow key
VK_UP		Up Arrow key
VK_RIGHT	Right Arrow key
VK_DOWN		Down Arrow key
VK_SELECT	Select key
VK_PRINT	Print key (keyboard-specific)
VK_EXECUTE	Execute key
VK_SNAPSHOT	Print Screen key
VK_INSERT	Insert key
VK_DELETE	Delete key
VK_HELP		Help key
VK_LWIN		Left Windows key (Microsoft keyboard)
VK_RWIN		Right Windows key (Microsoft keyboard)
VK_APPS		Applications key (Microsoft keyboard)
VK_NUMPAD0	0 key (numeric keypad)
VK_NUMPAD1	1 key (numeric keypad)
VK_NUMPAD2	2 key (numeric keypad)
VK_NUMPAD3	3 key (numeric keypad)
VK_NUMPAD4	4 key (numeric keypad)
VK_NUMPAD5	5 key (numeric keypad)
VK_NUMPAD6	6 key (numeric keypad)
VK_NUMPAD7	7 key (numeric keypad)
VK_NUMPAD8	8 key (numeric keypad)
VK_NUMPAD9	9 key (numeric keypad)
VK_MULTIPLY	Multiply key (numeric keypad)
VK_ADD		Add key (numeric keypad)
VK_SEPARATOR	Separator key (numeric keypad)
VK_SUBTRACT	Subtract key (numeric keypad)
VK_DECIMAL	Decimal key (numeric keypad)
VK_DIVIDE	Divide key (numeric keypad)
VK_F1		F1 key
VK_F2		F2 key
VK_F3		F3 key
VK_F4		F4 key
VK_F5		F5 key
VK_F6		F6 key
VK_F7		F7 key
VK_F8		F8 key
VK_F9		F9 key
VK_F10		F10 key
VK_F11		F11 key
VK_F12		F12 key
VK_F13		F13 key
VK_F14		F14 key
VK_F15		F15 key
VK_F16		F16 key
VK_F17		F17 key
VK_F18		F18 key
VK_F19		F19 key
VK_F20		F20 key
VK_F21		F21 key
VK_F22		F22 key
VK_F23		F23 key
VK_F24		F24 key
VK_NUMLOCK	Num Lock key
VK_SCROLL	Scroll Lock key
VK_LSHIFT	Left Shift key (only used with GetAsyncKeyState and GetKeyState)
VK_RSHIFT	Right Shift key  (only used with GetAsyncKeyState and GetKeyState)
VK_LCONTROL	Left Ctrl key  (only used with GetAsyncKeyState and GetKeyState)
VK_RCONTROL	Right Ctrl key  (only used with GetAsyncKeyState and GetKeyState)
VK_LMENU	Left Alt key  (only used with GetAsyncKeyState and GetKeyState)
VK_RMENU	Right Alt key  (only used with GetAsyncKeyState and GetKeyState)
VK_PROCESSKEY	Process key
VK_ATTN		Attn key
VK_CRSEL	CrSel key
VK_EXSEL	ExSel key
VK_EREOF	Erase EOF key
VK_PLAY		Play key
VK_ZOOM		Zoom key
VK_NONAME	Reserved for future use
VK_PA1		PA1 key
VK_OEM_CLEAR	Clear key

Hope this helps.
 

Thank you, guys. Especially paubri for his comprehensive help.

It only remains for me to do the job now. I will post my results, Giovanni, as soon as I've got some. (little children don't leave you much time to do things..)
 

Still haven't done it... but I've thought of another thing I probably need... the mouse clicks. I need to disable the mouse and/or handle the clicks....

:)

(snuck in ten minutes and at this very moment the child is bashing my arm and forcing me out of the seat)
 

Well I got something done and it half works. I didn't get to try disabling keypresses with the Peekmessage() thing but I used VK_Key and I found that some are recognised and some are not.

My programme wouldn't recognise and/or act upon VK_F1 or any other function key.

Worked well with the ESC key though. Which surprised me but delighted me. If keypresses are sent to the prog before anywhere else then I can catch everything... no problem.. and I can just do case else end. i.e. do nothing.

Wouldn't that work? I wouldn't need to have the programme actually do anything to move on to the next keypress. I don't really need to clear the buffer or put in some piece of go-nowhere code like the Peekmessage do I?

Same with the mouse I guess. If there's on Onclick or something (there's got to be) I can just do Onclick do nothing and we're right until the next click.

Surprise, surprise, my son is demanding attention. didn't we have a hassle today... screamed at me in the supermarket because he wanted to be carried, wouldn't sit in the shopping cart or anything else...

gotta go... :)
 
Hi

"Same with the mouse I guess. If there's on Onclick or something (there's got to be) I can just do Onclick do nothing and we're right until the next click."

.... and what's if the click is outbound of your form?
as an example if your baby right clicks on status bar?

as you Know, babies are .... terrible .....

Giovanni Caramia
 

Ah.. right.. now we're getting to it. I'm so stupid. Windows multi tasking programming is so foreign to me.

Yes, that's the whole point. I want him locked in to his programme and nothing else. That's right. I don't want the 'Windows' key or clicking outside the programme window to allow anything else to happen.

Doesn't windows perhaps have something after the style of the foreground and background processing thing that would allow only one application programme to run?

Why do I make so much of this when it's just a little app for my child? Because I'm learning by it, of course.

Nobody every worries about running just one app do they? The whole thing with Windows was/is to run many at once. I'm kind of trying to undo Windows I suppose. Can it be done? :)

You know babies? We've got another one the way, too....
 
In the good old doys days a made a piano program with pascal, but in your case, I would say, try to keep him occupied.
You could put a lot of pictures and tie sounds (wav etc. to them). Give him the mouse to play. The chance that he would punch in the correct key strokes to format the hard drive is very remote.

Babies are exploring the world, so they look at action and reaction.
My daughter (years ago) discovered that the led of the computer turned on and off (and drive me [evil]), pushing the button. Guess what happened, a little terrorist sneaking in and when I was busy.

How did I solve it? Showed her to use paint brush, Big Icon on the screen, and I had a two year old infant occupied [pipe]

Steven
 

That's what I'm doing - putting pictures and sounds with the keypresses.

The problem I'm trying to address now - and I've sort of wasted everyone's time from the beginning of this thread getting down to an accurate description of it - is 'how to keep him in the programme?'

I don't much care if he presses keys that do nothing. I don't much care how long it takes me to provide pictures and sounds for all the individual keys. I don't worry about him formatting the hard drive - or doing any damage any other way, really. I just want him there, in the prog, hacking away and from time to time getting responses I've programmed.

But currently he can't play for a couple of minutes without he's got the Start menu up or some other such thing.. he's lost the programme completely.

So - how to keep people locked into your prog? I've had my computer hung up often enough by some prog that I can't get out of. So it happens somehow, rightly or wrongly.

I've got an interest in this from a purely programming point of view, regardless of this little prog for Henry.

Maybe i'm a control freak?

:)
 
What about the procedure processmessages? We use it to prevent lock-up of Windows when doing lengthy operations

From the delphi Help:
Interrupts the execution of an application so that it can process the message queue.


procedure ProcessMessages;


Description

Call ProcessMessages to permit the application to process messages that are currently in the message queue. ProcessMessages cycles the Windows message loop until it is empty, and then returns control to the application.

Note: Neglecting message processing affects only the application calling ProcessMessages, not other applications. In lengthy operations, calling ProcessMessages periodically allows the application to respond to paint and other messages.
Note: ProcessMessages does not allow the application to go idle, whereas HandleMessage does.

Maybe you have to go this way.

What about hiding the windows statusbar?

Steven
 
I can be wrong but why don't you setup a separate user for your baby?? I mean this is what's windows XP is all about! my 4 year old son and 1 year old daughter have their own (limited) account and they didn't do any damage to pc (watercooled P4 2.8GHz@3.6GHz) whatsoever. I can limit their access to only a few games that are unharmful. My son started from his first birthday on my computer and 3 years later I don't have to explain a lot of things anymore about the PC. there are commercial toys/programs (like the one from bechet) that do exactly what you are trying to make. Offcourse writing your own program to do this costs nothing and can be very learnful. I'm just trying to inform here. happy programming anyway :)
[peace]

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Here is the basis of an unit code that you can use to develope your program.

The form is always on top and it's 'full-screen'. So every keypress and every mouse-click can easily controlled from your code.

For start you have to create a new project, drag a button on it and past this code on the unit.

You must activate these form-events: FormPaint and FormClose.

If some problems, let me know.
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;

  procedure showTaskbar;
  procedure hideTaskbar;
  procedure FormPaint(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Tform1.hideTaskbar;
var
wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle:=FindWindow(@wndClass[0], nil);
ShowWindow(wndHandle, SW_HIDE);
end;

procedure Tform1.showTaskbar;
var
wndHandle : THandle;
wndClass : array[0..50] of Char;
begin
StrPCopy(@wndClass[0], 'Shell_TrayWnd');
wndHandle:=FindWindow(@wndClass[0], nil);
ShowWindow(wndHandle, SW_RESTORE);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  HideTaskbar;
  application.ProcessMessages;
  FormStyle := fsStayOnTop;
  Borderstyle := bsNone;
  windowstate := wsMaximized;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ShowTaskbar;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  close;
end;

end.

Giovanni Caramia
 
Add Button1click event. You need it for 'close' the form and terminate the program.

Giovanni Caramia
 
Giovanni, you have hidden the statusbar, the close button and maximized the form, the only thing left for baby is to throw the towel [sleeping2] or [machinegun] [pc], when he is bored.
A star..

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top