Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with KeyDown
#1
For some reason, my computer doesn't recognize user input when I am blitting...Any suggestions(detailed stuff is below)...

First off, this involves 2 projects. The first project has a form with 4 lines drawn on it. It also has a Label. The code for it is this
Code:
Private Sub Block()
Line1.Visible = False
Line2.Visible = False
Line3.Visible = False
Line4.Visible = False
End Sub


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyRight
    Label1.Caption = "Right"
    Block
    Line4.Visible = True
    
    Case vbKeyLeft
    Label1.Caption = "Left"
    Block
    Line1.Visible = True
    
    Case vbKeyUp
    Label1.Caption = "Up"
    Block
    Line3.Visible = True
    
    Case vbKeyDown
    Label1.Caption = "Down"
    Block
    Line2.Visible = True
    
    End Select
End Sub

Private Sub Form_Load()
Block
End Sub
It simply displays which key you pressed and then displays a line in that direction.

The next project involves Blitting. I have 2 pic boxes. One has a 32 x 32 image in it of a sprite. I blit this onto the other picbox and try to get input from the user. Using almost the exact same code as before, I try to get key input. But, it doesn't work at all...
The project also has a timer on it.
Code:
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Dim SX As Integer
Dim SY As Integer

Private Sub BlitStan(ByVal X As Long, ByVal Y As Long)
BitBlt picMain.hDC, 32 * SX, 32 * SY, 32, 32, picStan.hDC, 0, 0, vbSrcPaint
End Sub

Private Sub Form_Load()
SX = 0
SY = 0
BlitStan SX, SY
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyRight
        SX = SX + 1
    Case vbKeyLeft
        SX = SX - 1
    Case vbKeyUp
        SY = SY - 1
    Case vbKeyDown
        SY = SY + 1
    End Select
End Sub

Private Sub timMain_Timer()
picMain.Cls
BlitStan SX, SY
picMain.Refresh
End Sub
Reply
#2
Did you make sure KeyPreview is set to = true?
Reply
#3
...wow. I never knew that that existed. Problem fixed. Thanks Spodi.

Also, is there a more efficient way to check user input from arrow keys?(other than key down or up)
Reply
#4
GetAsyncKeyState + GetActiveWindow is my current favorite.

Code:
Public Declare Function GetKeyState Lib "User32" (ByVal nVirtKey As Long) As Integer

Public Declare Function GetActiveWindow Lib "User32" () As Long

GetKeyState returns non-zero if the key has been pressed, ie:

Code:
If GetAsyncKeyState(vbKeyLeft) Then Msgbox ":)" else msgbox ":("

GetActiveWindow returns 0 if the current window is not selected. So you can add this to the above example before it:

Code:
If GetActiveWindow = 0 Then Exit Sub

Then you only get keypresses from when your project is active. Not sure how it works with multiple forms, but it works in MDI forms, I know that much. I'm pretty sure it'll work fine, though. :wink:

You have to use this with a timer of some sort since you have to constantly check for the key presses, they're not event based like the KeyDown and KeyPress events on the form.
Reply
#5
Thanks Spodi! I am trying to learn some basics before I do anything and this helps alot!
Reply
#6
Not a problem. 8)
Reply
#7
This'll help me too, thanks!

(Hiys Stomach Pulser, lots of people are on here.Tongue)

~Braydok/Elec0
Reply
#8
Wewt, two for the price of one! :lol:
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)