m.4.r.m
سه شنبه 31 مرداد 1391, 15:06 عصر
یه ماژول پیدا کردم گفتم بزارم همه استفاده کنیم :
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'Purpose : Test the Keyboard buffer to see which keys have been pressed
'Inputs : vKey1 The character or ascii value of the first key to test
' [vKey2] The character or ascii value of the second key to test
' To call using text use:
' Debug.Pring KeyPressed("S") 'Returns true if the user has the "S" keypressed
' To call using ASCII values use:
' Debug.Pring KeyPressed(vbKeyS) 'Returns true if the user has the "S" keypressed
'Outputs : Returns True if the specified Key/s where pressed
'Examples : To Find out if Control + Q has been pressed use:
' If KeyPressed(vbKeyControl ,vbKeyQ) Then
' or To Find out if Shift + Q has been pressed use:
' If KeyPressed(vbKeyShift ,vbKeyQ) Then
' or To Find out if Alt + Q has been pressed use:
' If KeyPressed(18,vbKeyQ) Then
'Revisions :
Public Function KeyPressed(ByVal vKey1 As Variant, Optional ByVal vKey2 As Variant) As Boolean
Dim bResult As Boolean
'Check First Key (need to call twice, the first call clears
'the keyboard buffer then second checks it's still pressed)
On Error GoTo ErrFailed
If VarType(vKey1) = vbString Then
bResult = CBool(GetAsyncKeyState(Asc(vKey1)))
bResult = CBool(GetAsyncKeyState(Asc(vKey1)))
Else
bResult = CBool(GetAsyncKeyState(vKey1))
bResult = CBool(GetAsyncKeyState(vKey1))
End If
If IsMissing(vKey2) = False Then
'Check Second Key
If VarType(vKey2) = vbString Then
bResult = bResult And CBool(GetAsyncKeyState(Asc(vKey2)))
bResult = bResult And CBool(GetAsyncKeyState(Asc(vKey2)))
Else
bResult = bResult And CBool(GetAsyncKeyState(vKey2))
bResult = bResult And CBool(GetAsyncKeyState(vKey2))
End If
End If
KeyPressed = bResult
Exit Function
ErrFailed:
Debug.Print Err.Description
Debug.Assert False
End Function
نحوه استفاده از این تابع :
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyPressed(vbKeyScrollLock, vbKeyEscape) Then
Form2.Show
End If
End Sub
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
'Purpose : Test the Keyboard buffer to see which keys have been pressed
'Inputs : vKey1 The character or ascii value of the first key to test
' [vKey2] The character or ascii value of the second key to test
' To call using text use:
' Debug.Pring KeyPressed("S") 'Returns true if the user has the "S" keypressed
' To call using ASCII values use:
' Debug.Pring KeyPressed(vbKeyS) 'Returns true if the user has the "S" keypressed
'Outputs : Returns True if the specified Key/s where pressed
'Examples : To Find out if Control + Q has been pressed use:
' If KeyPressed(vbKeyControl ,vbKeyQ) Then
' or To Find out if Shift + Q has been pressed use:
' If KeyPressed(vbKeyShift ,vbKeyQ) Then
' or To Find out if Alt + Q has been pressed use:
' If KeyPressed(18,vbKeyQ) Then
'Revisions :
Public Function KeyPressed(ByVal vKey1 As Variant, Optional ByVal vKey2 As Variant) As Boolean
Dim bResult As Boolean
'Check First Key (need to call twice, the first call clears
'the keyboard buffer then second checks it's still pressed)
On Error GoTo ErrFailed
If VarType(vKey1) = vbString Then
bResult = CBool(GetAsyncKeyState(Asc(vKey1)))
bResult = CBool(GetAsyncKeyState(Asc(vKey1)))
Else
bResult = CBool(GetAsyncKeyState(vKey1))
bResult = CBool(GetAsyncKeyState(vKey1))
End If
If IsMissing(vKey2) = False Then
'Check Second Key
If VarType(vKey2) = vbString Then
bResult = bResult And CBool(GetAsyncKeyState(Asc(vKey2)))
bResult = bResult And CBool(GetAsyncKeyState(Asc(vKey2)))
Else
bResult = bResult And CBool(GetAsyncKeyState(vKey2))
bResult = bResult And CBool(GetAsyncKeyState(vKey2))
End If
End If
KeyPressed = bResult
Exit Function
ErrFailed:
Debug.Print Err.Description
Debug.Assert False
End Function
نحوه استفاده از این تابع :
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyPressed(vbKeyScrollLock, vbKeyEscape) Then
Form2.Show
End If
End Sub