Jump to content

how to detect key input


Recommended Posts

im creating a typing game... and im afraid i dont know how to detect key input when its not the one im looking for.

lets say i have "J" up, and you're supposed to press "J", the _IsPressed() for "J" is "4A". i have it so when its clicked, points are added.

how would i have it so, if any other keys are hit, a function is done.

i had it so the if/then _ispressed statement is called, the else of subtracting points would not work when only keys are pressed, it would work on an infinite loop.

heres some code...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
Global $letters[26][2]= [["A", "41"], _
                        ["B", "42"], _
                        ["C", "43"], _
                        ["D", "44"], _
                        ["E", "45"], _
                        ["F", "46"], _
                        ["G", "47"], _
                        ["H", "48"], _
                        ["I", "49"], _
                        ["J", "4A"], _
                        ["K", "4B"], _
                        ["L", "4C"], _
                        ["M", "4D"], _
                        ["N", "4E"], _
                        ["O", "4F"], _
                        ["P", "50"], _
                        ["Q", "51"], _
                        ["R", "52"], _
                        ["S", "53"], _
                        ["T", "54"], _
                        ["U", "55"], _
                        ["V", "56"], _
                        ["W", "57"], _
                        ["X", "58"], _
                        ["Y", "59"], _
                        ["Z", "5A"]]


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 400, 257, 391, 206)
$Label1 = GUICtrlCreateLabel("", 20, 72, 360, 82, $SS_CENTER)
GUICtrlSetFont(-1, 52, 400, 0, "Arial Narrow")
GUICtrlSetColor(-1, 0x008a00)
$Label2 = GUICtrlCreateLabel("Score:", 8, 8, 35, 17)
$Label3 = GUICtrlCreateLabel("Time Left:", 8, 232, 51, 17)
$Button1 = GUICtrlCreateButton("Start", 320, 192, 75, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Stop", 320, 224, 75, 25, $WS_GROUP)
$Label4 = GUICtrlCreateLabel("", 56, 8, 76, 17)
$Label6 = GUICtrlCreateLabel("", 56, 25, 76, 17)
$Label5 = GUICtrlCreateLabel("7 second(s)", 64, 232, 70, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

$dll = DllOpen("user32.dll")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1

            _StartTyping()
    EndSwitch
WEnd

Func _StartTyping()
    While 1
        $randomeletter = Random(0, 25, 1)
        GUICtrlSetData($Label1, $letters[$randomeletter][0])
        $timer = TimerInit()
        While TimerDiff($timer) < 7000
            GUICtrlSetData($Label5, Round((7000-TimerDiff($timer))/1000, 0) & " second(s)")
            Sleep(62)
            If GUIGetMsg() = $Button2 Then ExitLoop 2
            If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
            If _IsPressed($letters[$randomeletter][1],$dll) Then
                GUICtrlSetData($Label4, GUICtrlRead($Label4) + Round((7000-TimerDiff($timer))/1000, 0)*25)
                GUICtrlSetData($Label6, "+ " & Round((7000-TimerDiff($timer))/1000, 0)*25)
                ExitLoop 1
            EndIf
            
            ; this is when you click the wrong key.
;~              GUICtrlSetData($Label4, GUICtrlRead($Label4) - Round((7000-TimerDiff($timer))/1000, 0)*25)
;~              GUICtrlSetData($Label6, "- " & Round((7000-TimerDiff($timer))/1000, 0)*25)
            
        WEnd
    WEnd
EndFunc
Edited by billthecreator

[font=Microsoft Sans Serif]My Scripts: From Most recent to least.[/font]Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap

Link to comment
Share on other sites

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
Global $letters[26][2] = [["A", "41"], _
        ["B", "42"], _
        ["C", "43"], _
        ["D", "44"], _
        ["E", "45"], _
        ["F", "46"], _
        ["G", "47"], _
        ["H", "48"], _
        ["I", "49"], _
        ["J", "4A"], _
        ["K", "4B"], _
        ["L", "4C"], _
        ["M", "4D"], _
        ["N", "4E"], _
        ["O", "4F"], _
        ["P", "50"], _
        ["Q", "51"], _
        ["R", "52"], _
        ["S", "53"], _
        ["T", "54"], _
        ["U", "55"], _
        ["V", "56"], _
        ["W", "57"], _
        ["X", "58"], _
        ["Y", "59"], _
        ["Z", "5A"]]


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 400, 257, 391, 206)
$Label1 = GUICtrlCreateLabel("", 20, 72, 360, 82, $SS_CENTER)
GUICtrlSetFont(-1, 52, 400, 0, "Arial Narrow")
GUICtrlSetColor(-1, 0x008a00)
$Label2 = GUICtrlCreateLabel("Score:", 8, 8, 35, 17)
$Label3 = GUICtrlCreateLabel("Time Left:", 8, 232, 51, 17)
$Button1 = GUICtrlCreateButton("Start", 320, 192, 75, 25, $WS_GROUP)
$Button2 = GUICtrlCreateButton("Stop", 320, 224, 75, 25, $WS_GROUP)
$Label4 = GUICtrlCreateLabel("", 56, 8, 76, 17)
$Label6 = GUICtrlCreateLabel("", 56, 25, 76, 17)
$Label5 = GUICtrlCreateLabel("7 second(s)", 64, 232, 70, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1

            _StartTyping()
    EndSwitch
WEnd

Func _StartTyping()
    While 1
        $randomeletter = Random(0, 25, 1)
        GUICtrlSetData($Label1, $letters[$randomeletter][0])
        $timer = TimerInit()
        While TimerDiff($timer) < 7000
            GUICtrlSetData($Label5, Round((7000 - TimerDiff($timer)) / 1000, 0) & " second(s)")
            Sleep(62)
            If GUIGetMsg() = $Button2 Then ExitLoop 2
            If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
            If _IsPressed($letters[$randomeletter][1]) Then
                While _AnyPressed()
                    Sleep(10)
                WEnd
                GUICtrlSetData($Label4, GUICtrlRead($Label4) + Round((7000 - TimerDiff($timer)) / 1000, 0) * 25)
                GUICtrlSetData($Label6, "+ " & Round((7000 - TimerDiff($timer)) / 1000, 0) * 25)
                ExitLoop
            Else
                If _AnyPressed() Then
                    While _AnyPressed()
                        Sleep(10)
                    WEnd
                    GUICtrlSetData($Label4, GUICtrlRead($Label4) - Round((7000 - TimerDiff($timer)) / 1000, 0) * 25)
                    GUICtrlSetData($Label6, "- " & Round((7000 - TimerDiff($timer)) / 1000, 0) * 25)
                    ExitLoop
                EndIf
            EndIf
        WEnd
    WEnd
EndFunc   ;==>_StartTyping

Func _AnyPressed()
    For $i = 0 To UBound($letters) - 1
        If _IsPressed($letters[$i][1]) Then
            Return 1
        EndIf
    Next
    Return 0
EndFunc   ;==>_AnyPressed

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...