Solved

KeyDown issue: cannot seem to capture it

Posted on 2008-06-18
9
171 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…

789 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