---
title: "如何识别键盘左右的shift,Ctrl或Alt键 "
author: "cjc"
date: "2004-08-03T18:21:00Z"
tags: []
slug: "shift-ctrl-alt"
draft: false
lastmod: "2004-08-03T18:21:00Z"
id: 299174
url: "https://cjc.iteye.com/blog/299174"
---

用API （GetAsyncKeyState）实现，在WIN 2K，XP 下有效：

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer  
Private Const VK\_LSHIFT = &HA0  
Private Const VK\_RSHIFT = &HA1  
Private Const VK\_LCTRL = &HA2  
Private Const VK\_RCTRL = &HA3  
Private Const VK\_LMENU = &HA4  
Private Const VK\_RMENU = &HA5

Private Sub Form\_KeyDown(KeyCode As Integer, Shift As Integer)  
If KeyCode = vbKeyShift And Shift = vbShiftMask And (GetAsyncKeyState(VK\_LSHIFT) And &H8000) Then MsgBox "you click the Left Shift"  
If KeyCode = vbKeyShift And Shift = vbShiftMask And (GetAsyncKeyState(VK\_RSHIFT) And &H8000) Then MsgBox "you click the RIGHT Shift"

If KeyCode = vbKeyMenu And Shift = vbAltMask And (GetAsyncKeyState(VK\_LMENU) And &H8000) Then MsgBox "you click the Left ALT" ' only ALT was pressed  
If KeyCode = vbKeyMenu And Shift = vbAltMask And (GetAsyncKeyState(VK\_RMENU) And &H8000) Then MsgBox "you click the RIGHT ALT" ' only ALT was pressed

If KeyCode = vbKeyControl And Shift = vbCtrlMask And (GetAsyncKeyState(VK\_LCTRL) And &H8000) Then MsgBox "you click the Left CTRL"  
If KeyCode = vbKeyControl And Shift = vbCtrlMask And (GetAsyncKeyState(VK\_RCTRL) And &H8000) Then MsgBox "you click the Right CTRL"

End Sub

Private Sub Form\_Load()  
Me.KeyPreview = True  
End Sub