Solved

KeyDown issue: cannot seem to capture it

Posted on 2008-06-18
9
179 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Suggested Courses
Course of the Month5 days, 6 hours left to enroll

636 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