Solved

KeyDown issue: cannot seem to capture it

Posted on 2008-06-18
9
167 Views
Last Modified: 2010-07-27
Visual Basic 6
Windows Application

I have a simple slot machine type app that I run on a projector through a book pc.
It is just a cutesy program but it cannot be run with the mouse, it needs key input.

When you press "s" it runs, pressing the ESC key ends the program
it works flawlessly on the first try, but after the program executes once it will not recognize another keypress. There is nothing set when it executres, no focus or tab stops or anything, it just execute a shockwave flash object and then displays something in a text box.

Onbce it does that, I no longer get response to keypress events.

I do not know if this is a focus issue, tabstop or????

This is the code I have in the KeyDown

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 69 Then
End
ElseIf KeyCode = 101 Then
End
ElseIf KeyCode = 8 Then
End
ElseIf KeyCode = 13 Then
End
ElseIf KeyCode = 27 Then
End
ElseIf KeyCode = 115 Then
Picture1_Click
ElseIf KeyCode = 83 Then
Picture1_Click
Else
End If
End Sub
0
Comment
Question by:EGormly
  • 5
  • 3
9 Comments
 
LVL 7

Expert Comment

by:TRobinJames
ID: 21814665
you said: "and then displays something in a text box."  

This will take focus away from Form and then form.keydown event will not fire
0
 

Author Comment

by:EGormly
ID: 21814755
TRobinJames:

Although I appreciate the response, that is not helpful.
Perhaps I did not explain properly (sorry), I want a fix, not a reason.

I need to know how to get the program to continue monitoring the KeyDown event.
I can't simply remove the textbox event.
0
 
LVL 7

Expert Comment

by:TRobinJames
ID: 21815366
The focus thing in VB has messed me up many times.  I think the problem is with using the Form to catch the keydown.  Add another control (ie,picturebox) that is transparent so it is invisible to the user but can take focus.  In form.activate set the focus to the picturebox and catch all keydowns with the picturebox.keydown.  It is important to always put the focus back to the catching picturebox.  In your code, when picturebox1_click is called it will take the focus away (I dont know where it goes), so you will need to set it back to the catching picturebox to continue to catch keys.  
0
 

Author Comment

by:EGormly
ID: 21815685
If I set ANY focus with .setfocus it fails
The program doesn't work at all. it does not recognize ANY input.
Once I take the focus out of form.activate it works again.

I followed your advice, created a transparent picturebox, used form.activate to picture1.SetFocus
and put my code in the new picturebox control and then it just wouldn't recognize any key input.  Take it out, the program works fine (until it runs the first time)


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:TRobinJames
ID: 21815861
"used form.activate to picture1.SetFocus"

You cant use picture1 as this is in use in your code

ElseIf KeyCode = 115 Then
Picture1_Click
ElseIf KeyCode = 83 Then
Picture1_Click
Else
End If
End Sub


In any event, create a new project with a form, a textbox and two picture boxes and try the code below.  It should place "R in the text box every time a key is pressed.  Works well on my XP machine with VB6 SP6.  Not sure what other wrinkles you have that may not allow to work.
Private Sub Form_Activate()

    Picture2.SetFocus

End Sub
 
 

Private Sub Picture1_Click()

    Call pic1

    Picture2.SetFocus

End Sub
 

Private Sub Picture2_KeyDown(KeyCode As Integer, Shift As Integer)
 

   Text1.Text = Text1.Text & "R "

   Picture1_Click

   Picture2.SetFocus

End Sub
 

Private Sub pic1()

    Picture1.Refresh

End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 21816017
Can't you just set the Forms KeyPreview() Property to True and be done with it?...
0
 

Author Comment

by:EGormly
ID: 21821352
Idle_Mind:
"Can't you just set the Forms KeyPreview() Property to True and be done with it?..."

I tried that... it didn't change anything.

However, once I put in:

Text1.SetFocus
and then added this after
Form1.KeyPreview = True

it worked ! Thanks!
0
 

Author Comment

by:EGormly
ID: 21821365
TRobinJames:
Thank you for all the effort at helping, I do appreciate it.  
I am not sure why your suggestions did not work, it seemed like it should, perhaps it was just my crappy code..
0
 

Author Closing Comment

by:EGormly
ID: 31468437
Hasd to tweak this but it only took a few seconds after this suggestion to get it to work!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Spreadsheet - Send email if certain value is selected. 14 86
Excel object stays open 19 71
VB6 Compile Compatibility Issue 4 89
Best way to parse out a json string in VB6? 10 111
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now