Solved

VB 6 KeyPress event not responding...

Posted on 2004-08-19
18
1,266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 34

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 34

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 34

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
 
LVL 34

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 34

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 34

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 34

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 34

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month3 days, 21 hours left to enroll

630 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