fox125 fox125 - 5 months ago 16
Vb.net Question

Vb.net register a CTRL-combo with an asyncstate function

I am trying to make a calculator that is fully-controlled with the keypad.

I had some trouble with getting the keystrokes on the keypad registered but eventualy I used a GetAsyncKeystate function

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

For i = 1 To 255
result = 0

result = GetAsyncKeyState(i)

If result = -32767 Then
Select Case i
Case Is = 96
Display.Text = Display.Text & "0"
ans = ans & "0"
Case Is = 97
Display.Text = Display.Text & "1"
ans = ans & "1"
Case Is = 98
Display.Text = Display.Text & "2"
ans = ans & "2"
Case Is = 99
Display.Text = Display.Text & "3"
ans = ans & "3"
Case Is = 100
Display.Text = Display.Text & "4"
ans = ans & "4"
Case Is = 101
Display.Text = Display.Text & "5"
ans = ans & "5"
Case Is = 102
Display.Text = Display.Text & "6"
ans = ans & "6"
Case Is = 103
Display.Text = Display.Text & "7"
ans = ans & "7"
Case Is = 104
Display.Text = Display.Text & "8"
ans = ans & "8"
Case Is = 105
Display.Text = Display.Text & "9"
ans = ans & "9"
Case Is = 106
Multiply()
Case Is = 107
Add()
Case Is = 110
Display.Text = Display.Text & ","
ans = ans & ","
Case Is = 109
Substract()
Case Is = 111
Devide()
Case Is = 13 And ActionValue <> ""

Action()

Display.Text = ans2
Display.Text = Display.Text & Environment.NewLine

ans = ans2
ans2 = ""

End Select

End If

Next i

End Sub


Now I don't know how I should register a Ctrl-combo (like Ctrl + NUM-LOCK)
Btw I'm not used to the GetsAsyncKeyState function

Answer

You first need to check if the CTRL-Key or any other modifier-key like Alt or whatever is pressed at the beginning. (refer to http://msdn.microsoft.com/en-us/library/dd375731%28VS.85%29.aspx for the keycodes). Define variables like

Dim isCTRLPressed as Boolean = (GetAsyncKeyState(&HA2) < 0)

When processing the actual keys you can then fork the respective action

If isCTRLPressed Then
  DoThis()
Else
  DoThat()
End If

If you meant something else you might elaborate on your post some more. Your code is also rather inefficient since you forcefully check every keystate on every Tick wether it is actually used or not.