Quantcast
Viewing all articles
Browse latest Browse all 27221

Keypress Event not firing

I currently have a form with a panel on it and then 100 pictureboxes inside the panel. I was using the keydown event to 'move' my map, which worked. I was doing something like this:
vb.net Code:
  1. Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
  2.         If e.KeyCode = Keys.Left AndAlso map(current_possition.X - 1, current_possition.Y).IsPassable AndAlso player.IsMoving = False Then
  3.             current_possition.X -= 1
  4.             player.CurrentLocation = current_possition
  5.             player.player_move()
  6.             Call load_view()
  7.         ElseIf e.KeyCode = Keys.Right AndAlso map(current_possition.X + 1, current_possition.Y).IsPassable AndAlso player.IsMoving = False Then
  8.             current_possition.X += 1
  9.             Call load_view()
  10.         ElseIf e.KeyCode = Keys.Up AndAlso map(current_possition.X, current_possition.Y - 1).IsPassable AndAlso player.IsMoving = False Then
  11.             current_possition.Y -= 1
  12.             Call load_view()
  13.         ElseIf e.KeyCode = Keys.Down AndAlso map(current_possition.X, current_possition.Y + 1).IsPassable AndAlso player.IsMoving = False Then
  14.             current_possition.Y += 1
  15.             Call load_view()
  16.         End If
  17.     End Sub
The problem came when the user would just hold down the keys. It created really bad jumping, which is why I added the player.IsMoving = False, but that doesn't matter if the user holds the key down as the player wasn't moving when the key was origionally down. I've tried moving it to the keypress thinking that will solve my problem, and it looks almost identical to the key-down:
vb.net Code:
  1. Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
  2.         If e.KeyChar = ChrW(Keys.Left) AndAlso map(current_possition.X - 1, current_possition.Y).IsPassable AndAlso player.IsMoving = False Then
  3.             current_possition.X -= 1
  4.             player.CurrentLocation = current_possition
  5.             player.player_move()
  6.             Call load_view()
  7.         ElseIf e.KeyChar = ChrW(Keys.Right) AndAlso map(current_possition.X + 1, current_possition.Y).IsPassable AndAlso player.IsMoving = False Then
  8.             current_possition.X += 1
  9.             Call load_view()
  10.         ElseIf e.KeyChar = ChrW(Keys.Up) AndAlso map(current_possition.X, current_possition.Y - 1).IsPassable AndAlso player.IsMoving = False Then
  11.             current_possition.Y -= 1
  12.             Call load_view()
  13.         ElseIf e.KeyChar = ChrW(Keys.Down) AndAlso map(current_possition.X, current_possition.Y + 1).IsPassable AndAlso player.IsMoving = False Then
  14.             current_possition.Y += 1
  15.             Call load_view()
  16.         End If
  17.     End Sub

The problem is I set a breakpoint at the key_press, but it never fires. Any ideas as to why?


Edit - I do indeed have keypreview set to true.

Viewing all articles
Browse latest Browse all 27221

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>