Solved

Enable Disable buttons from keyboard.

Posted on 2008-10-29
25
729 Views
Last Modified: 2011-10-19
I develop an application and i want to make sure that the user will not type some letters and the enter button from the keyboard at a specific time because he/she will save wrong statistics in a table.Those keys that i want to know how to enable and then disable are the letters "o" and "t" and the enter button.

Be careful i don't want to disable or enable only for the form input but i want a general windows disable or enable of the buttons.For example if the user open a notepad and goes to type the o or the t or to hit the enter button i want not to be able to do this.

Thanks in advance!
0
Comment
Question by:alex_code
  • 10
  • 6
  • 3
  • +2
25 Comments
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 22835928
well you can redirect the keyboard event to the FORM;
set the form's KeyPreview property to True; whenever you want to capture any keyboard event the component still sees the event, but the form has an opportunity to handle it first - to allow or disallow some keys...
Suppose you have several Edit components on a form and the Form.OnKeyPress procedure looks like:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char) ;
begin
 if Key in ['0'..'9'] then Key := #0
end;

If one of the Edit components has the Focus and KeyPreview property of a form is False, this code will not execute - in other words if the user presses the '5' key the '5' character will appear in the focused Edit component.
However if the KeyPreview is set to True than the form's OnKeyPress event is executed before the Edit component sees the key that is pressed. Again if the user has pressed the '5' key, the if assigns the character value of zero to Key to prevent numerical input into the Edit component.


hope this helps
0
 
LVL 28

Expert Comment

by:ciuly
ID: 22835937
you need to create a keyboard hook. using the winapi is sufficient for your case.

here are a few links for your reference with code and explanations afor different scenarios:

http://delphi.about.com/od/windowsshellapi/a/keyboard_hook.htm
http://www.delphifaq.com/faq/delphi_windows_API/f512.shtml
http://www.delphi3000.com/articles/article_3390.asp

let me know if yuo have any questions
0
 
LVL 9

Author Comment

by:alex_code
ID: 22836252
ciuly yes i have questions.Those examples seems to be ok but it is little complex and it is not clear how can i set that i want to disable or enable the keys that i want (o,t,enter button).
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 22837198
I would think a keyboard hook would be overkill, we were not talking about stopping keyboard events globally from outside your application but from within your application on a couple events from a component who had focus
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 22838673
the Enter key may be a problem when you have a BitBtn or Button on your form
with Default = True
You could catch this in the OnClick event or Disable the Button

disabling the letters 'o' and 't' and enter in a TEdit


procedure TForm1.OnEdit1KeyPress(Sender: TEdit; var Key: Char);

begin

  if FlagSet then 

    if Key in [#13,'o','O','t','T'] then

      Key := #0;

end;

Open in new window

0
 
LVL 9

Author Comment

by:alex_code
ID: 22838938
Guys thanks for the posts but i said how can i general disable and enable those buttons.I know how to disable or enable buttons  on my form with keypress events but i want to be totally sure that the user will not press these keys so i want something like keyboard hook as ciuly said.
0
 
LVL 28

Expert Comment

by:ciuly
ID: 22839095
I took the delphifaq demo and modified it for your needs. also removed a lot of garbage you didn't need.
here is the demo: http://www.ciuly.com/delphi/win32api/hooks/keyboardHook/index.html
this disables the keys at all times while the hook is active. you will need to adapt this for your need. one way to do it so that you don't complicate the hook itself, is to enable and siable the hook only when you want the keys to be disabled ;)
0
 
LVL 16

Expert Comment

by:CodedK
ID: 22840710
Hi Alex_Code

I've uploaded a zip file. Inside you'll find the project group to build the
dll and the exe to do exactly what you need.
Project1 - > Dll
Project2 - _ exe

Inside your project put the dll in the uses clause like in my project2...

Change the ShowMessage with a beep or with nothing or whatever you like.
Seperate the onKeyDown and OnKeyUp if you dont want to get double messages.
Thats about it.

Hope this helps :)

0
 
LVL 9

Author Comment

by:alex_code
ID: 22840757
hi codedk i have a question.... where is the f....ing zip file?    :)
0
 
LVL 16

Accepted Solution

by:
CodedK earned 500 total points
ID: 22840764
Here it is....
It was invisible... :P
hook.txt
0
 
LVL 9

Author Comment

by:alex_code
ID: 22840779
oh you are a real Guru you upload invisible files....
0
 
LVL 16

Expert Comment

by:CodedK
ID: 22840788
Alex_Code its a rar file...
Dont brake the computer to open it ! :D
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Author Comment

by:alex_code
ID: 22840939
Ciuly i try to run your project but i get an error of the dll file.



ohhh yeah Codedk just a small mod on your dll and everything works perfect thanks a lot man!!!
0
 
LVL 28

Expert Comment

by:ciuly
ID: 22841260
since I uploaded a zip, it obviously worked for me. which means the error might be because o different delphi versions or whatever.

in any case:
- you didn't tell me what error and
- you didn't give me any chance to fix it, even though it wasn't my fault

so .. good luck getting help from me in the future.
0
 
LVL 9

Author Comment

by:alex_code
ID: 22841968
Ciuly i had download your zip file and the dll was missing so when i run it i got an error and i don't know what to do about this or how to make a dll for this project.All this happens few minutes before Codedk's  post me a perfect solution that works, what i must to do?I must not to accept  Codedk's solution that was a full solution and wait for you to make a new version of your code to work properly?Just tell me and if i was wrong i'll make a post to Experts carry to split the points i don't have any problem to do that.

P.S.

so .. good luck getting help from me in the future. <-- Ok nop if you help people here because it is your pleasure and no only for points i think that you will help me in the future but if you don't i want to thank you for all the solutions you gave me until now.
0
 
LVL 9

Author Comment

by:alex_code
ID: 22841981
sorry i did a mistake
experts carry ---> experts care
0
 
LVL 28

Expert Comment

by:ciuly
ID: 22842045
the zip contains the dprs: the project and the dll. all you needed to do is build both or ASK. anyway, lazyness, or whatever, don't matter for me. I'm sick and tired of such people who don't respect my time.
I treat you the way you treat me: you don't give me a chance, then I don't give you a chance either. it's that simple. what goes around, comes around.

sayonara
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 22842143
>>ciuly
don't be too harsh on this guy
he can't help that some of the recent askers are only giving B grades
(i'm getting some (newbies) too at the moment) and he happened to do this once too.

I think that some newbies should first read everything and pass and exam before asking questions
and there should be a grading system for askers ...
or a button for the experts to accept the grade or not

0
 
LVL 9

Author Comment

by:alex_code
ID: 22842154
--->  all you needed to do is build both or ASK  <---

I didn't knew it that i have to build it and i suppose you had to tell me that into your post and not to expect to ask it so your solution was incomplete.

pantofla!
0
 
LVL 28

Expert Comment

by:ciuly
ID: 22842272
>> he can't help that some of the recent askers are only giving B grades

this has nothing to do with that. unless he gave you a B grade (which only you, the askers and moderators can see ;) )

this one, like others, is about me giving them fully functional solutions, and they not having what it takes to use it (I don't want to say the real truth here since netminder will be all over me, again)

who in their sane mind would think that you need to tell a delphi developer to compile all dpr's, after given a few links which clearly explain that there is a dll and an exe involved in the project. get it? I give the guy some good links with good explanations, he needs some help with the code, I give him the code and his just too **** to use it.

it's like a guy goes into a bar and says he's thirsty. the bartender gives him a glass of water and after a few hours this guy dies of thurst. turns out the guy was too *** and he needed to be told to pick up the glass, move it to his mounth and pour the water in his mounth, not forgetting to swallow once in a while.

I'm out.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 22842456
>> (which only you, the askers and moderators can see ;) )
if you're good at math ... you can calculate what grade you got ... more or less

0
 
LVL 28

Expert Comment

by:ciuly
ID: 22842477
>> if you're good at math ... you can calculate what grade you got ... more or less

provided your answer was at least assisted :P and if it was, you get an email telling you the grade anyhow. none of which is the case with me here ;)
0
 
LVL 9

Author Comment

by:alex_code
ID: 22842712
Ciuly,

First of all nobody in this world is perfect and nobody can criticize other people about what they know and what they don't.I was a begginer on this kind of problem if you read it...

~~~~~~~~~~
alex_code, Qualified Expert
Beginner on this subject.
~~~~~~~~~~

I am 26 years old and i programming (professionally) in delphi about 2 years on many different big projects (databases,capi,remote admin, etc) and i collect experience by the years so i can now do some mistakes and to have the ability to correct them.i think until now you also do mistakes and i don't think you are inerrable.You aren't a delphi programmer from the first day you borned you learn throw your mistakes, mistakes are exists to learn!!! I am not new in Experts i had also help many people on several zones and i do it because it is my pleasure to help anyone which has a problem and not to collect points to catch someone that is in a higher position of me in the rank!

I realize there are many askers here that need to take a lesson ... BUT there also exist some *** experts that along with their points gain idiocy.

i think that EE would be much better place without people like you ... that feel better by insulting people they dont know !
0
 
LVL 9

Author Comment

by:alex_code
ID: 22842972
If you want to see how to answer see these link of Russel's answer.

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23850905.html#a22834967

He explains to the asker what to do step by step.
I don't think that Russel do that because he thinks that the asker is an idiot, but he is a perfect programmer and he wants to be clear about his answer.
An answer with fully functional, something that you are not make practice of. You give incomplete answers just to post before other and earn points.

Bye,bye...
0
 
LVL 4

Expert Comment

by:LMuadDIb
ID: 22843141
if your a pro delphi programmerr then you are not a beginner and should not have to be given baby steps...

the only way a person learns is actually doing some of the work yourself.
If it is given to you compeltely step by step you will never learn anything.


in my original post I was going to mention using a keyboard hook, but I didnt becuase I thought for a beginner it would not be an easy task... unless you wanted the complete code handed to you that is........

sorry but I had to say something, it was disrepectful
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

20 Experts available now in Live!

Get 1:1 Help Now