VB.NET Detect Key Presses

This bit of code will allow you to detect key presses while the form is not the active window. I currently use this for macros and for my trainer programs for turning on cheats.

All you need to have is a timer on the form. You can either drag and drop one on the form or declare it in code and write the timer.Tick event.

In this example I check for 2 key presses.

  1. Dim altkey As Boolean
  2. Dim Num_1 As Boolean
  3. Dim Num_2 As Boolean
  4. Dim Num_3 As Boolean
  5.  
  6. Private Sub tmr_Keys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_Keys.Tick
  7. altkey = GetAsyncKeyState(Keys.LMenu)
  8. Num_1 = GetAsyncKeyState(Keys.NumPad1)
  9. Num_2 = GetAsyncKeyState(Keys.NumPad2)
  10. Num_3 = GetAsyncKeyState(Keys.NumPad3)
  11. If altkey And Num_1 = True Then 'Infinite Health Player 1
  12. btn_P1InfiniteHealth_Click(sender, e)
  13. ElseIf altkey And Num_2 = True Then
  14. btn_P1InfiniteKi_Click(sender, e) ' Infinite Ki Player 1
  15. ElseIf altkey And Num_3 = True Then
  16.  
  17. End If
  18. End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *