Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,655 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,158 people online right now. Registration is fast and FREE... Join Now!




Keyboard is too fast

 
Reply to this topicStart new topic

Keyboard is too fast

prolulzibus
post 15 Aug, 2008 - 02:07 PM
Post #1


New D.I.C Head

*
Joined: 15 Aug, 2008
Posts: 5


My Contributions


Here's a problem I've had for some time. This is the section of my code which checks if the user presses a key, and moves a sprite along the map according to which button is pressed. If the user presses the "Numpad5" key, it switches on and off some text.

CODE
            Case 5
                info *= -1

(info is an integer I used as a substitute for a boolean, if info = 1 then it prints the text)


When I press Numpad5, the keyboard polls its own state so fast that if I hold the key down for even a moment the text flickers on and off really quickly.

Here is the code. With as little change as possible, how can I slow down the rate of the keyboard's poll?
CODE
Private Sub CheckInput()
        Dim keynum As Integer
        keyboard.Poll()

        If movetile > 0 Then
            If keyboard.KeyState(Key.NumPad1) Then keynum = 1
            If keyboard.KeyState(Key.NumPad2) Then keynum = 2
            If keyboard.KeyState(Key.NumPad3) Then keynum = 3
            If keyboard.KeyState(Key.NumPad4) Then keynum = 4
            If keyboard.KeyState(Key.NumPad5) Then keynum = 5
            If keyboard.KeyState(Key.NumPad6) Then keynum = 6
            If keyboard.KeyState(Key.NumPad7) Then keynum = 7
            If keyboard.KeyState(Key.NumPad8) Then keynum = 8
            If keyboard.KeyState(Key.NumPad9) Then keynum = 9
        End If

        Select Case keynum
            Case 1
                MoveSpriteY(spdy)
                MoveSpriteX(-spdx)
                movetile -= 1
            Case 2
                MoveSpriteY(spdy)
                movetile -= 1
            Case 3
                MoveSpriteY(spdy)
                MoveSpriteX(spdx)
                movetile -= 1
            Case 4
                MoveSpriteX(-spdx)
                movetile -= 1
            Case 5
                info *= -1
            Case 6
                MoveSpriteX(spdx)
                movetile -= 1
            Case 7
                MoveSpriteY(-spdy)
                MoveSpriteX(-spdx)
                movetile -= 1
            Case 8
                MoveSpriteY(-spdy)
                movetile -= 1
            Case 9
                MoveSpriteY(-spdy)
                MoveSpriteX(spdx)
                movetile -= 1
        End Select


        If keyboard.KeyState(Key.Escape) Then
            keyboard = Nothing
            mouse = Nothing
            scroller = Nothing
            sprite1 = Nothing
            d3d = Nothing
            clock.Stop()
            Me.Close()
        End If
    End Sub


CheckInput is called in the main function, perhaps I need to alter the function call instead.

This isn't a homework assignment, so don't hold back! Any level of complexity is welcome. Thanks guys.

User is offlineProfile CardPM

Go to the top of the page

KYA
post 15 Aug, 2008 - 02:15 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,211



Thanked 50 times

Dream Kudos: 1150
My Contributions


This is VB, so I don't have a handy solution right off the bat (I'm more of a C/C++ guy). What sort of timing/timer do you have setup within the game's framework?

edit: It looks like clock(), is there a pause or delay feature you have implemented?

This post has been edited by KYA: 15 Aug, 2008 - 02:16 PM
User is offlineProfile CardPM

Go to the top of the page

prolulzibus
post 15 Aug, 2008 - 02:28 PM
Post #3


New D.I.C Head

*
Joined: 15 Aug, 2008
Posts: 5


My Contributions


QUOTE(KYA @ 15 Aug, 2008 - 03:15 PM) *

edit: It looks like clock(), is there a pause or delay feature you have implemented?


It is indeed a clock()... what if I increased or decreased clock.Interval? Or maybe not... I'm not entirely familiar with Timers, that's why this is a problem.

Well, that and my lack of experience!

This post has been edited by prolulzibus: 15 Aug, 2008 - 02:29 PM
User is offlineProfile CardPM

Go to the top of the page

KYA
post 15 Aug, 2008 - 02:31 PM
Post #4


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,211



Thanked 50 times

Dream Kudos: 1150
My Contributions


ok, so clock is an internal/built in component of a VB class?

Ideally, you would have a GameLoop() that would execute x amount of times/second which would give you steady input handling and frames per second, etc...
User is offlineProfile CardPM

Go to the top of the page

stayscrisp
post 15 Aug, 2008 - 02:33 PM
Post #5


D.I.C Regular

***
Joined: 14 Feb, 2008
Posts: 258



Thanked 6 times
My Contributions



hmmmm you say you have some kind of boolean value, what about a new one that checks whether the key has been released

then you could only press the key again if the key has been released, i did this for a game because i had the same problem, unfortunately i can't give you any example code as i do not know any visual basic syntax :s

but basically when the key is pressed it will change a value to true then it will only be able to be pressed again if the key has been released smile.gif

try that, ask me any questions if you don't know what i mean

icon_up.gif
User is offlineProfile CardPM

Go to the top of the page

prolulzibus
post 15 Aug, 2008 - 02:51 PM
Post #6


New D.I.C Head

*
Joined: 15 Aug, 2008
Posts: 5


My Contributions


Wow, that makes all the difference... thanks.

CODE

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If iskeydown = False Then
            iskeydown = True
        ElseIf iskeydown = True Then
            iskeydown = False
        End If
    End Sub


CODE

            Case 5
                If iskeydown = True Then
                    info *= -1
                End If


That was exactly what I was looking for.

This post has been edited by prolulzibus: 15 Aug, 2008 - 02:52 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:27AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month