标题: 如何识别键盘左右的shift,Ctrl或Alt键
lonjew
元帅
Rank: 1



UID 73191
精华 1
积分 30615
帖子 14941
威望 3
金币 14931
热心 12
阅读权限 100
注册 2008-1-28
状态 离线
如何识别键盘左右的shift,Ctrl或Alt键

  用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

顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-11 10:23
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP