Solved

KeyDown issue: cannot seem to capture it

Posted on 2008-06-18
9
166 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

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 about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

11 Experts available now in Live!

Get 1:1 Help Now