Results 1 to 10 of 10

Thread: Keypress Event Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    16

    Unhappy

    Hello, we are trying to make a game application where one of the users presses a button when he/she knows the answer.

    Here is the code we have experimented with:

    If Chr(KeyAscii) = "a" Or Chr(KeyAscii) = "A" Then
    msgbox "you pressed A"

    End If

    this code snippet works in a new project, but for some reason it will not work in ours.
    This might be because there are other procedures going on
    I.E. a timer we have counting down. Please help us if you know how we can make this work properly.

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I can't tell you when you don't post some code, but maybe this code is easier to read

    Code:
    If UCase(Chr(KeyAscii) = "A") Then msgbox "you pressed A"
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    New Member
    Join Date
    Oct 2000
    Posts
    7

    Lightbulb

    If you think the timer is preventing the execution of that code snippet you could disable then enable the timers with the keydown and keyup events. usin this structure:

    Form1_KeyDown(KeyCode as Integer, Shift as Integer)
    Timer1.enabled = false
    'print the message over here using your code snippet
    End Sub

    Form1_KeyUp(KeyCode as Integer, Shift as Integer)
    Timer1.enabled = True
    End Sub

    What this does is ensure that the timers are not working as for the short moment when the key is pressed. If you have more than one timer insert this procedure to disable all of them in the keydown event.

    Public Sub DisableTimers()
    For each Control in Form1.controls
    If TypeOf Control Is Timer then
    control.enabled = false
    End if
    Next Control

    To then re-enable the timers call this procedure in the key up event.

    Public Sub EnableTimers()
    For each Control in Form1.controls
    If TypeOf Control Is Timer then
    control.enabled = true
    End if
    Next Control


    Hope this works if you have probs with it mail me at [email protected] .....I'll be glad to help, I have made a game making heavy use of timers before..I know it can get quite hassling. Enjoy.

  4. #4
    New Member
    Join Date
    Oct 2000
    Posts
    7

    Lightbulb

    If you think the timer is preventing the execution of that code snippet you could disable then enable the timers with the keydown and keyup events. usin this structure:

    Form1_KeyDown(KeyCode as Integer, Shift as Integer)
    Timer1.enabled = false
    'print the message over here using your code snippet
    End Sub

    Form1_KeyUp(KeyCode as Integer, Shift as Integer)
    Timer1.enabled = True
    End Sub

    What this does is ensure that the timers are not working as for the short moment when the key is pressed. If you have more than one timer insert this procedure to disable all of them in the keydown event.

    Public Sub DisableTimers()
    For each Control in Form1.controls
    If TypeOf Control Is Timer then
    control.enabled = false
    End if
    Next Control

    To then re-enable the timers call this procedure in the key up event.

    Public Sub EnableTimers()
    For each Control in Form1.controls
    If TypeOf Control Is Timer then
    control.enabled = true
    End if
    Next Control


    Hope this works if you have probs with it mail me at [email protected] .....I'll be glad to help, I have made a game making heavy use of timers before..I know it can get quite hassling. Enjoy.

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I don't think that'll work, because if you have a timer with a interval like 1 minute, and the timer was at 58 seconds completed, when you disable and enable the Timer again and you'll get a huge delay.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  6. #6
    New Member
    Join Date
    Oct 2000
    Posts
    7
    The delay would only be a couple of milliseconds though.Ok post some more code up and I'll suggest some other idea.Tomorrow.I just need to see exactly what you want your app to do.

  7. #7
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    Try this:
    Code:
    If KeyAscii = 65 Or KeyAscii = 97 Then
        MsgBox "you pressed a!"
    End If
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  8. #8
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372

    You really must post code!

    You can't be helped unless you show us your code. Two other things you could try:
    1. Set the forms keypreview to true
    2. If you have an intensive loop running somewhere add a DoEvents in it. This will allow the keypress procedure to run

    Two simple methods

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2000
    Posts
    16

    Ok, here is some code

    This is what I want to do:

    This is a two person trivia game where a question is displayed then the counter counts down from 10 ( the first person that rings in (presses A for player 1 and L for player 2) will be prompted to enter the correct answer.

    Here is the code for the timer:


    Amount = Val(lbltimer.Caption)
    If Amount = 0 Then
    Timer1.Enabled = False
    Else
    Amount = Amount - 1
    lbltimer.Caption = Str$(Amount)
    End If
    'when enabled this timer counts down from 10
    If lbltimer.Caption = 0 Then
    lblquestion.Caption = "Time's Up"
    End If
    If Val(lbltimer.Caption) >= 1 Then
    keypressaq
    End If

    now sometime during this counter is counting down, a user that knows the correct answer would press either A or L (depending on whether they are player 1 or 2)


    All i need now is how to put in the code that will make the answer form show when the user presses there ring-in button.
    Thanks.
    Drew


  10. #10
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I don't see your problem man, this code works for me, give it a try:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If UCase(KeyCode) = vbKeyA Then
    MsgBox "You pressed A!"
    Timer1.Enabled = False
    End If
    End Sub
    
    Private Sub Form_Load()
    Label1.Caption = "10"
    KeyPreview = True
    End Sub
    
    Private Sub Timer1_Timer()
    Amount = Val(Label1.Caption)
    If Amount = 0 Then
    Timer1.Enabled = False
    Else
    Amount = Amount - 1
    Label1.Caption = Str$(Amount)
    End If
    'If Val(Label1.Caption) >= 1 Then
    'keypressaq
    'End If
    End Sub
    Just put a label and a timer on your form and run it!
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width