微分在keydown事件
问题描述:
输入键之间如果使用该keydown处理压在VB.net可以检测回车键:微分在keydown事件
Private Sub kd(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
IF e.key=key.Enter Then
<do your code>
Endif
End Sub
然而,有一个方法告诉这两个输入键在键盘被按下?既然你有两个键盘(一个接一个地撇号(中间)和一个在数字键盘),我想有中间的输入按钮具有数字键盘不同的响应回车键输入键。
一个例子:在Photoshop中,用文本工具时,可以按中间进入添加文本的另一条线,或按小键盘进入确认你输入的内容。
答
从http://support.microsoft.com/kb/188550/en-us
代码在VB.net
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (ByRef lpMsg As MSG, ByVal hwnd As Integer, ByVal wMsgFilterMin As Integer, ByVal wMsgFilterMax As Integer, ByVal wRemoveMsg As Integer) As Integer
Private Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Private Structure MSG
Dim hwnd As Integer
Dim message As Integer
Dim wParam As Integer
Dim lParam As Integer
Dim time As Integer
Dim pt As POINTAPI
End Structure
Const PM_NOREMOVE As Integer = &H0
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Integer = &H101
Const VK_RETURN As Integer = &HD
Private Sub Form1_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim KeyCode As Short = eventArgs.KeyCode
Dim Shift As Short = eventArgs.KeyData \ &H10000
Dim MyMsg As MSG
Dim RetVal As Integer
' pass:
' MSG structure to receive message information
' my window handle
' low and high filter of 0, 0 to trap all messages
' PM_NOREMOVE to leave the keystroke in the message queue
' use PM_REMOVE (1) to remove it
RetVal = PeekMessage(MyMsg, Me.Handle.ToInt32, 0, 0, PM_NOREMOVE)
' now, per Q77550, you should look for a MSG.wParam of VK_RETURN
' if this was the keystroke, then test bit 24 of the lparam - if ON,
' then keypad was used, otherwise, keyboard was used
If RetVal <> 0 Then
If MyMsg.wParam = VK_RETURN Then
If MyMsg.lParam And &H1000000 Then
MsgBox("Enter from Keypad pressed")
Else
MsgBox("Enter from Keyboard pressed")
End If
End If
Else
MsgBox("No message waiting, or possible problems calling PeekMessage")
End If
End Sub
End Class
采取