Hack Forums
[.NET] How can I make user set hotkeys to press a button? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] How can I make user set hotkeys to press a button? (/showthread.php?tid=4841590)



How can I make user set hotkeys to press a button? - ermin2300 - May 29, 2015

Hi, i'm trying to make a soundboard and I can make buttons play a sound, but I want the user to be able to select a key, and everytime he presses that key, the sound will play.

Anyone know how to do this?


* - wizardwizard - May 29, 2015

Okey.
Timer needs to have 10 ms delay or less, you can change the key by selecting a key in Keys.<KEYHERE>.
IF you want the user to set the key, try a combo box of all the keys.
PHP Code

<?php 
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      
If GetAsyncKeyState(Keys.A) Then
      Button1
.PerformClick()
      
System.Threading.Thread.Sleep(100)
      
End If
End Sub 



* - Odus - May 29, 2015

If this is global, look into RegisterHotKey with PInvoke. Just remember to free it on FormClosing.


* - ermin2300 - May 30, 2015

(May 29, 2015 12:16 PM)dmratcliffe Wrote:  Okey.
Timer needs to have 10 ms delay or less, you can change the key by selecting a key in Keys.<KEYHERE>.
IF you want the user to set the key, try a combo box of all the keys.
PHP Code

<?php 
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      
If GetAsyncKeyState(Keys.A) Then
      Button1
.PerformClick()
      
System.Threading.Thread.Sleep(100)
      
End If
End Sub 

Thank you sir, appreciated.


* - ermin2300 - Jun 3, 2015

(May 29, 2015 12:16 PM)dmratcliffe Wrote:  Okey.
Timer needs to have 10 ms delay or less, you can change the key by selecting a key in Keys.<KEYHERE>.
IF you want the user to set the key, try a combo box of all the keys.
PHP Code

<?php 
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
      
If GetAsyncKeyState(Keys.A) Then
      Button1
.PerformClick()
      
System.Threading.Thread.Sleep(100)
      
End If
End Sub 

However when I press A it doesn't want to play the sound?
I've added the code you gave to the timer but nothing happens when I press A or any other button if I set it to another?


* - LUOIdeRentA - Jun 3, 2015

PHP Code

<?php 
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Integer
    Dim hotkey 
As Boolean

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Timer1
.Enabled = True
      Timer1
.Interval = 100
    End Sub

    
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      hotkey 
= GetAsyncKeyState(Keys.A)
      If 
hotkey = True Then
      Button1
.PerformClick()
      
System.Threading.Thread.Sleep(100)
      
End If
    
End Sub 

There you go, hope this helps


* - ermin2300 - Jun 5, 2015

(Jun 3, 2015 09:00 AM)thevegita730 Wrote:  
PHP Code

<?php 
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Integer
    Dim hotkey 
As Boolean

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Timer1
.Enabled = True
      Timer1
.Interval = 100
    End Sub

    
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      hotkey 
= GetAsyncKeyState(Keys.A)
      If 
hotkey = True Then
      Button1
.PerformClick()
      
System.Threading.Thread.Sleep(100)
      
End If
    
End Sub 

There you go, hope this helps

Thank you so much got it working now!!


(Jun 3, 2015 09:00 AM)thevegita730 Wrote:  
PHP Code

<?php 
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Integer
    Dim hotkey 
As Boolean

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Timer1
.Enabled = True
      Timer1
.Interval = 100
    End Sub

    
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      hotkey 
= GetAsyncKeyState(Keys.A)
      If 
hotkey = True Then
      Button1
.PerformClick()
      
System.Threading.Thread.Sleep(100)
      
End If
    
End Sub 

There you go, hope this helps

Thank you so much got it working now!!