Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

KeyDown issue: cannot seem to capture it

Posted on 2008-06-18
9
Medium Priority
?
188 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses

664 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