Problems with KeyChar

i have all the things that happen when a key is pressed but theres a p

Page 1 of 1

5 Replies - 1905 Views - Last Post: 04 March 2007 - 12:27 AM Rate Topic: -----

#1 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Problems with KeyChar

Posted 02 March 2007 - 07:41 PM

ok so heres whats rong i have under the form1_keypress sub i have all the key chars for the keys i want but what happens if i try to click both at the same time only the latter one happens. or say i was holding down a then hit d it would only registar "d" does anyone know how to fix this... heres my code
private sub form1_keypress()
if e.KeyCHar = "w" then
' do something
end if
if e.KeyChar = "d' then
' do something
end if
end sub


thnx
john

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with KeyChar

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Problems with KeyChar

Posted 02 March 2007 - 07:49 PM

if you are holding the 'a' key and then press the 'd' key, d is the only press event, while a becomes a down or release event, which are treated differntly in all languages.
It's been a while since i've done key presses in VB, but the events are the same, so perhaps someone else will have a more specific answer.
Was This Post Helpful? 0
  • +
  • -

#3 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Problems with KeyChar

Posted 02 March 2007 - 07:54 PM

View PostWilliam_Wilson, on 2 Mar, 2007 - 07:49 PM, said:

if you are holding the 'a' key and then press the 'd' key, d is the only press event, while a becomes a down or release event, which are treated differntly in all languages.
It's been a while since i've done key presses in VB, but the events are the same, so perhaps someone else will have a more specific answer.

i guess ya theres the whole hold and realese thing but the real trick is having so form1_keypress or form1_keyup ect. that it can be used for the a and d key at the same time and i was wondering if anyone knew how to do that (or if it can even be done)
thnx
john
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Problems with KeyChar

Posted 03 March 2007 - 05:47 AM

CAN it be done? Yes. Here is what I have come up with.
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_UP = &H26
Private Const VK_A = &H41
Private Const VK_D = &H44

Private Sub Form_Load()
	Timer1.Interval = 500
End Sub


Private Sub Command1_Click()
	Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Timer1_Timer()
	Dim i As Integer
	Text1.Text = Hex$(GetAsyncKeyState(VK_A))
	Text2.Text = Hex$(GetAsyncKeyState(VK_D))
End Sub


The form for this code contains two textboxes, a button, and a timer. Clicking the button will start the program which will scan the A and D keys.

The referance on this is
GetAsyncKeyState
Virtual-Key Codes
Was This Post Helpful? 0
  • +
  • -

#5 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: Problems with KeyChar

Posted 03 March 2007 - 01:39 PM

thnx but thats not really my problem my problem is i have this code with two pictureboxes who can move using w s d a and 8 4 6 2 but i only one person can move at a time
heres my code
Public Class Form1
	Dim left1 As Integer
	Dim left2 As Integer
	Dim top1 As Integer
	Dim top2 As Integer
	Dim change As Integer
	Dim glitch As Boolean
	Dim g1 As Boolean
	Dim g2 As Boolean
	Dim g3 As Integer
	Dim g4 As Boolean
	Dim g5 As Boolean
	Dim istrue As Boolean
	Dim which As Integer
	Dim glitch2 As Boolean
	Dim shot1 As Boolean
	Dim shot2 As Boolean
	Private Sub setstuff()
		top1 = PictureBox1.Top
		left1 = PictureBox1.Left
		top2 = PictureBox2.Top
		left2 = PictureBox2.Left
	End Sub
	Private Sub setpicturebox()
		PictureBox1.Top = top1
		PictureBox1.Left = left1
		PictureBox3.Top = (PictureBox1.Top + 0)
		PictureBox3.Left = (PictureBox1.Left + 42)
		PictureBox2.Top = top2
		PictureBox2.Left = left2
		PictureBox4.Top = (PictureBox2.Top + 8)
		PictureBox4.Left = (PictureBox2.Left - 16)
		PictureBox5.Left = PictureBox3.Left
		PictureBox5.Top = PictureBox3.Top - 16
		PictureBox6.Left = PictureBox3.Left
		PictureBox6.Top = PictureBox3.Top + 16
	End Sub

	Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
		If e.KeyChar = "^" Then
			g4 = True
		End If
		If e.KeyChar = "&" Then
			If g4 = True Then
				g5 = True
			End If
		End If
		If e.KeyChar = "*" Then
			If g5 = True Then
				change = 10
			End If
		End If
		If e.KeyChar = "$" Then
			If g5 = True Then
				change = 30
			End If
		End If
		If e.KeyChar = "!" Then
			If g5 = True Then
				glitch2 = True
			End If
		End If
		If e.KeyChar = "~" Then
			g1 = True
		End If
		If e.KeyChar = "`" Then
			If g1 = True Then
				g2 = True
			End If
		End If
		If e.KeyChar = "@" Then
			If g2 = True Then
				glitch = True
			End If
		End If
		If e.KeyChar = "%" Then
			g3 = g3 + 1
			If g3 = 3 Then
				g1 = False
				g2 = False
				glitch = False
				g3 = 0
			End If
		End If
		If e.KeyChar = "w" Then
			top1 = top1 - change
			which = 1
		End If
		If e.KeyChar = "s" Then
			top1 = top1 + change
			which = 1
		End If
		If e.KeyChar = "a" Then
			left1 = left1 - change
			which = 1
		End If
		If e.KeyChar = "d" Then
			left1 = left1 + change
			which = 1
		End If
		If e.KeyChar = " " Then
			If glitch2 = True Then
				PictureBox5.Visible = True
				PictureBox6.Visible = True
			End If
			PictureBox3.Visible = True
			Timer1.Start()
		End If
		If e.KeyChar = "5" Then
			PictureBox4.Visible = True
			Timer2.Start()
		End If
		If e.KeyChar = "8" Then
			top2 = top2 - change
			which = 2
		End If
		If e.KeyChar = "2" Then
			top2 = top2 + change
			which = 2
		End If
		If e.KeyChar = "4" Then
			left2 = left2 - change
			which = 2
		End If
		If e.KeyChar = "6" Then
			left2 = left2 + change
			which = 2
		End If
		checkforbarriers()
		If istrue = False Then
endo:	   setpicturebox()
		End If
		istrue = False
	End Sub
	Private Sub checkforbarriers()
		If which = 1 Then
			If left1 > 85 And left1 < 146 And top1 > 81 And top1 < 180 Then
				istrue = True
				left1 = PictureBox1.Left
				top1 = PictureBox1.Top
			End If
			If left1 > 601 And left1 < 661 And top1 > 81 And top1 < 180 Then
				istrue = True
				left1 = PictureBox1.Left
				top1 = PictureBox1.Top
			End If
		End If
		If which = 2 Then
			If left2 > 601 And left2 < 661 And top2 > 69 And top2 < 181 Then
				istrue = True
				left2 = PictureBox2.Left
				top2 = PictureBox2.Top
			End If
			If left2 > 85 And left2 < 146 And top2 > 69 And top2 < 181 Then
				istrue = True
				left2 = PictureBox2.Left
				top2 = PictureBox2.Top
			End If
		End If
	End Sub
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Timer1.Interval = (1)
		Timer2.Interval = (1)
		change = 3
		setstuff()
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		shot1 = True
		If glitch = False Then
			If glitch2 = False Then
				PictureBox3.Left = PictureBox3.Left + 7
			End If
			If glitch2 = True Then
				PictureBox3.Left = PictureBox3.Left + 7
				PictureBox5.Left = PictureBox5.Left + 7
				PictureBox6.Left = PictureBox6.Left + 7
			End If
		Else
			If glitch2 = False Then
				PictureBox3.Left = PictureBox3.Left + 20
			End If
			If glitch2 = True Then
				PictureBox3.Left = PictureBox3.Left + 20
				PictureBox5.Left = PictureBox5.Left + 20
				PictureBox6.Left = PictureBox6.Left + 20
			End If
		End If
		If PictureBox3.Left > 725 Then
			Timer1.Stop()
			PictureBox3.Top = (PictureBox1.Top + 0)
			PictureBox3.Left = (PictureBox1.Left + 42)
			PictureBox3.Visible = False
			PictureBox5.Top = (PictureBox1.Top + 0)
			PictureBox5.Left = (PictureBox1.Left + 42)
			PictureBox5.Visible = False
			PictureBox6.Top = (PictureBox1.Top + 0)
			PictureBox6.Left = (PictureBox1.Left + 42)
			PictureBox6.Visible = False
			shot1 = False
		End If
		If PictureBox3.Left > PictureBox2.Left Then
			If PictureBox3.Top > (PictureBox2.Top - 1) And PictureBox3.Top < (PictureBox2.Top + 42) Then
				Timer1.Stop()
				Timer2.Stop()
				Panel1.Visible = True
				MsgBox("Black Won")
				GoTo endof
			End If
		End If

		If PictureBox5.Left > PictureBox2.Left Then
			If PictureBox5.Top > (PictureBox2.Top - 1) And PictureBox5.Top < (PictureBox2.Top + 42) Then
				Timer1.Stop()
				Timer2.Stop()
				Panel1.Visible = True
				MsgBox("Black Won")
				GoTo endof
			End If
		End If

		If PictureBox6.Left > PictureBox2.Left Then
			If PictureBox6.Top > (PictureBox2.Top - 1) And PictureBox6.Top < (PictureBox2.Top + 42) Then
				Timer1.Stop()
				Timer2.Stop()
				Panel1.Visible = True
				MsgBox("Black Won")
				GoTo endof
			End If
		End If
		If PictureBox3.Left > 110 And PictureBox3.Left < 145 Then
			If PictureBox3.Top > 109 And PictureBox3.Top < 180 Then
				Timer1.Stop()
				PictureBox3.Visible = False
				PictureBox3.Top = (PictureBox1.Top + 0)
				PictureBox3.Left = (PictureBox1.Left + 42)
			End If
		End If
		If PictureBox3.Left > 627 And PictureBox3.Left < 662 Then
			If PictureBox3.Top > 109 And PictureBox3.Top < 180 Then
				Timer1.Stop()
				PictureBox3.Visible = False
				PictureBox3.Top = (PictureBox1.Top + 0)
				PictureBox3.Left = (PictureBox1.Left + 42)
			End If
		End If
endof:
	End Sub

	Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
		PictureBox4.Left = PictureBox4.Left - 7
		If PictureBox4.Left < 2 Then
			Timer1.Stop()
			PictureBox4.Top = (PictureBox2.Top + 8)
			PictureBox4.Left = (PictureBox2.Left - 16)
			PictureBox4.Visible = False
		End If

		If PictureBox4.Left < PictureBox1.Left Then
			If PictureBox4.Top > (PictureBox1.Top - 1) And PictureBox3.Top < (PictureBox2.Top + 28) Then
				Timer1.Stop()
				Timer2.Stop()
				Panel1.Visible = True
				MsgBox("Blue Won")
			End If
		End If


		If PictureBox4.Left > 110 And PictureBox4.Left < 145 Then
			If PictureBox4.Top > 109 And PictureBox4.Top < 180 Then
				Timer2.Stop()
				PictureBox4.Visible = False
				PictureBox4.Top = (PictureBox2.Top + 8)
				PictureBox4.Left = (PictureBox2.Left - 16)
			End If
		End If
		If PictureBox4.Left > 627 And PictureBox4.Left < 662 Then
			If PictureBox4.Top > 109 And PictureBox4.Top < 180 Then
				Timer2.Stop()
				PictureBox4.Visible = False
				PictureBox4.Top = (PictureBox2.Top + 8)
				PictureBox4.Left = (PictureBox2.Left - 16)
			End If
		End If
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Close()
	End Sub
	Private Sub restart(ByVal Program As String)
		For Each proc As Process In Process.GetProcesses
			If proc.ProcessName = Program Then proc.Kill()
		Next
		Process.Start(Program)
	End Sub
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		restart("Game")
		Me.Close()
		PictureBox1.Left = 21
		PictureBox1.Top = 131
		PictureBox3.Left = 67
		PictureBox3.Top = 131
		PictureBox2.Left = 688
		PictureBox2.Top = 131
		PictureBox4.Left = 762
		PictureBox4.Top = 141
		g1 = False
		g2 = False
		g3 = 0
		glitch = False
		change = 3
		left1 = PictureBox1.Left
		left2 = PictureBox2.Left
		top1 = PictureBox1.Top
		top2 = PictureBox2.Top
		Panel1.Visible = False
		PictureBox3.Visible = False
		PictureBox4.Visible = False
	End Sub
End Class


ps some of the code is fore glitches
thnx
john
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Problems with KeyChar

Posted 04 March 2007 - 12:27 AM

Maybe using DoEvents would do the trick, although I'm not sure.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1