Solved

VB 6 KeyPress event not responding...

Posted on 2004-08-19
18
1,259 Views
Last Modified: 2010-05-18
I had some code in keypress event working fine.  For some reason it is no longer responding.  I used "Run/Stard with Full Compile" but no error is coming up (Btw, I have noticed this doesen't necessarily catch all the errors errors).

This morning, I was trying to capture cursor x and y position.  I tried to add some additional parameters in:

Private Sub cmdButton_KeyPress(KeyAscii As Integer)

to look like:

Private Sub cmdButton_KeyPress(KeyAscii As Integer, x as integer, y As Integer)   'may be dumb thing to do

It didn't work and gave me compile error (can't recall what it said).

I also played a bit with mouse move event because it already has x as integer and y As Integer.  Is it possible that this experiments has something to do with it?

I did a hard reboot; but it didn't help.  Do you have any idea what might have happend.

Thanks to your help.
0
Comment
Question by:Mike Eghtebas
  • 8
  • 5
  • 3
  • +1
18 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11846943
Does the button have the focus when you press the key?
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11847018
KeyPress does not capture mouse movement or mouse button clicks. This is done by MouseMove, MouseDwon and MouseUp events. The KeyPress, KeyDown and KeyUp events capture KEYBOARD keys while the user types.
What are you trying to achieve with the keypress events?

S
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11847401
I see what you've done. By adding a parameter to the event function you have changed a button event to a user defined function which will never run unless you call it yourself.

Like Shauli says if you want the mouse X, Y you need to code in the KeyDown or KeyUp events.

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11847480
I have given up on what I was trying to do by finding x and y position of cursor on keypress.

Before that, I had some code in Form_Keypress event that worked fine.  It no longer works.  I want get it working again.

Regards,

Mike
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11847725
For one. remove the x y from the keypress. It doesn't belong there. To do that, copy the code in that event and delete the event altogether, then recreate the event by selecting it from the events list, and paste the original code (without adding x y to the elements of the sub). This should bring the event back to life.
Secondly, as I mentioned above, the KeyPress event cannot capture mouse position, as it responses to keyboard strokes.

Let us know :)

S
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11848365
Hi Shauli,

Re:> For one. remove the x y from the keypress.

Have removed them.
---------
Re:>To do that, copy the code in that event and delete the event altogether,...

I did this this morning and once more after reading your response.  Didn't help.
-----------
Re:> Secondly, as I mentioned above, the KeyPress event cannot capture ...

I appreciate the info.  Cursor position is of no interest at this point and I have tried to remove all codes relating to this topic.  Maybe not all of it hence the problem.
----------
I added a new form to my project.  MsgBox KeyAscii prompts me with ascii nimber of key I press on this form.  However, it doesn't do in my old form with lots of code and controls on it.

I maybe need to transfer all my controls and code (mabe not all) to the new form?  What do you thing?

Regards,

Mike
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11848428
Can you post the code of the keypress event here?

S
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11848559
MsgBox KeyAscii
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11848609
No :) the code of the original keypress event. The one that doesn't work even after you've removed the x and y.

S
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11848753
I don't have problem with the code.  I have problem with this event not firring at all.  

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11848759
I have removed everything (deleted it).  Then, double-clicked on the form and located KeyPress event:

Private Sub Form_Keypress(KeyAscii As Integer)

  MsgBox KeyAscii

End Sub
0
 
LVL 19

Assisted Solution

by:Shauli
Shauli earned 150 total points
ID: 11848872
" I have removed everything (deleted it).  Then, double-clicked on the form and located KeyPress event:"

Have you tried to do that and then add the original code (the code that failed).

I don't think we are on the same page. I'm trying to find out why the original code fails, even after you've removed the x y. Or am I not on the right track?

S
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11848945
Re:> Have you tried to do that and then add the original code (the code that failed).

I don't have the original code in there (btw, this is the code I got from you in some other question where P executes Play of Windwos Media Player, etc).

Re:> I'm trying to find out why the original code fails

The original code never failed.  I can use it in another event fine.  Nevertheless, I want use it by a key stroke in Form_PressKey event.

Mike
0
 
LVL 7

Accepted Solution

by:
Enlade earned 350 total points
ID: 11848971

I think that you just have not set the KeyPreview property to True.  If you don't set the KeyPreview property of your form to true then the keypress will be passed on to whatever conrtol has focus (button, textbox, whatever).  However, if you set it to true then the form will get the first look at the keystroke and thus your keypress function will be processed.  That might also be the reason that you used to get it to work.  Either you didn't have a control that had focus (that is your form had focus) or you had the KeyPreview set to true originally.  Give it a try and see what it does for you.

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11848994

Yuck, that is bad English.  Basically, you should try setting the KeyPreview property of your Form to True.  Then see if your forms KeyPress event works for you again.  Hope that is clearer.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11849217
Hi Enlade,

That was the case.  Thank you.

For about a week, I have used this application and demo'ed it to my client using keypress events.  It was set to Ture days ago.  This is why Shauli never questioned this aspect of the problem (Shauli has been great help with this project).

Regards,

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11849225
I guess, I was messing around with the parameters in key press event; somehow, it must set keypreview to False.

Mike
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11851097

Yea, the big place were that property becomes important is when you are using MDI child forms and you want to control activity through keystrokes on the main application form (MDI form).  There are other ways to do the same thing by super/sub classing the application message handler, but this little flag (KeyPreview) does the trick for most things.  Glad to help.  Take care.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

24 Experts available now in Live!

Get 1:1 Help Now