0

我已经能够使用下面的方法切换 CAPS、NUM 和 SCROLL 锁定,但使用下面的代码虽然当我单击按钮时没有发生错误,但没有任何反应?

适用于 caps、num 和 scroll 的代码

Private Sub imgCONTROL_Click(sender As System.Object, e As System.EventArgs) Handles imgCONTROL.Click
    Call keybd_event(System.Windows.Forms.Keys.ControlKey, &H14, 1, 0)
    Call keybd_event(System.Windows.Forms.Keys.ControlKey, &H14, 3, 0)
End Sub

不起作用的代码,请注意我已经尝试过 Alt、AltKey 等的所有变体

Private Sub imgCONTROL_Click(sender As System.Object, e As System.EventArgs) Handles imgCONTROL.Click
    Call keybd_event(System.Windows.Forms.Keys.Alt, &H14, 1, 0)
    Call keybd_event(System.Windows.Forms.Keys.Alt, &H14, 3, 0)
End Sub

如果有人有解决方案,我将不胜感激。

4

1 回答 1

0

Note: This answer is untested. It's a quick stab at the problem.

The CAPS, NUM and SCROLL lock keys toggle when you press and release them.

The CTRL, ALT and SHIFT keys turn on when pressed and turn off when released. Your code presses and releases the key instantly. You need to write code to toggle the key each time the image is clicked.

Private Sub imageALT_Click(sender As Object, e As EventArgs) Handles imgALT.Click
    ' Toggle key state to pressed or released.
    Dim Static State As Boolean = False
    State = Not State
    ' Set the flags to press or release.
    Dim Flags As Integer = 1 ' KEYEVENTF_EXTENDEDKEY
    If State Then Flags = Flags Or 2 ' KEYEVENTF_KEYUP
    ' Press or release the key.
    Call keybd_event(System.Windows.Forms.Keys.Alt, &H14, Flags, 0)
End Sub
于 2011-12-06T00:18:20.123 回答