| Mirage Source http://web.miragesource.net/forums/ |
|
| Trouble with KeyDown http://web.miragesource.net/forums/viewtopic.php?f=201&t=1614 |
Page 1 of 1 |
| Author: | Stomach Pulser [ Thu Apr 05, 2007 6:18 am ] |
| Post subject: | Trouble with KeyDown |
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 |
|
| Author: | Spodi [ Thu Apr 05, 2007 6:37 am ] |
| Post subject: | |
Did you make sure KeyPreview is set to = true? |
|
| Author: | Stomach Pulser [ Thu Apr 05, 2007 3:44 pm ] |
| Post subject: | |
...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) |
|
| Author: | Spodi [ Thu Apr 05, 2007 5:27 pm ] |
| Post subject: | |
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. 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. |
|
| Author: | Stomach Pulser [ Thu Apr 05, 2007 6:00 pm ] |
| Post subject: | |
Thanks Spodi! I am trying to learn some basics before I do anything and this helps alot! |
|
| Author: | Spodi [ Thu Apr 05, 2007 7:05 pm ] |
| Post subject: | |
Not a problem. |
|
| Author: | Braydok [ Thu Apr 05, 2007 7:19 pm ] |
| Post subject: | |
This'll help me too, thanks! (Hiys Stomach Pulser, lots of people are on here. ~Braydok/Elec0 |
|
| Author: | Spodi [ Thu Apr 05, 2007 10:23 pm ] |
| Post subject: | |
Wewt, two for the price of one! |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|