VB 程序检测按键

摘要

VB 程序检测按键,检测所按下的按键,实现所希望动作

VB 程序检测按键 
弹出所按下键(使用KeyDown事件)

语法格式为: 

Sub Object_KeyDown(KeyCode As Integer,Shift As Integer)

例:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyUp Then msgbox "您按了上键!" ElseIf KeyCode = vbKeyDown Then msgbox "您按了下键!" ElseIf KeyCode = vbKeyLeft Then msgbox "您按了左键!" ElseIf KeyCode = vbKeyRight Then msgbox "您按了右键!" End If End Sub 

依此类推,可以设置为其键值 
下面提供VB中的各个按键常量! 
Keycode 常数 
可在代码中的任何地方用下列常数代替实际值:

常数         值     描述 
vbKeyLButton 0x1 鼠标左键 
vbKeyRButton 0x2 鼠标右键 
vbKeyCancel 0x3 CANCEL 键 
vbKeyMButton 0x4 鼠标中键 
vbKeyBack 0x8 BACKSPACE 键 
vbKeyTab 0x9 TAB 键 
vbKeyClear 0xC CLEAR 键 
vbKeyReturn 0xD ENTER 键 
vbKeyShift 0x10 SHIFT 键 
vbKeyControl 0x11 CTRL 键 
vbKeyMenu 0x12 MENU 键 
vbKeyPause 0x13 PAUSE 键 
vbKeyCapital 0x14 CAPS LOCK 键 
vbKeyEscape 0x1B ESC 键 
vbKeySpace 0x20 SPACEBAR 键 
vbKeyPageUp 0x21 PAGE UP 键 
vbKeyPageDown 0x22 PAGE DOWN 键 
vbKeyEnd 0x23 END 键 
vbKeyHome 0x24 HOME 键 
vbKeyLeft 0x25 LEFT ARROW 键 
vbKeyUp 0x26 UP ARROW 键 
vbKeyRight 0x27 RIGHT ARROW 键 
vbKeyDown 0x28 DOWN ARROW 键 
vbKeySelect 0x29 SELECT 键 
vbKeyPrint 0x2A PRINT SCREEN 键 
vbKeyExecute 0x2B EXECUTE 键 
vbKeySnapshot 0x2C SNAPSHOT 键 
vbKeyInsert 0x2D INSERT 键 
vbKeyDelete 0x2E DELETE 键 
vbKeyHelp 0x2F HELP 键 
vbKeyNumlock 0x90 NUM LOCK 键 
A 至 Z 键与 A – Z 字母的 ASCII 码相同:
常数 值 描述 
vbKeyA 65 A 键 
vbKeyB 66 B 键 
vbKeyC 67 C 键 
vbKeyD 68 D 键 
vbKeyE 69 E 键 
vbKeyF 70 F 键 
vbKeyG 71 G 键 
vbKeyH 72 H 键 
vbKeyI 73 I 键 
vbKeyJ 74 J 键 
vbKeyK 75 K 键 
vbKeyL 76 L 键 
vbKeyM 77 M 键 
vbKeyN 78 N 键 
vbKeyO 79 O 键 
vbKeyP 80 P 键 
vbKeyQ 81 Q 键 
vbKeyR 82 R 键 
vbKeyS 83 S 键 
vbKeyT 84 T 键 
vbKeyU 85 U 键 
vbKeyV 86 V 键 
vbKeyW 87 W 键 
vbKeyX 88 X 键 
vbKeyY 89 Y 键 
vbKeyZ 90 Z 键 09 键与数字 09 的 ASCII 码相同:
常数 值 描述 
vbKey0 48 0 键 
vbKey1 49 1 键 
vbKey2 50 2 键 
vbKey3 51 3 键 
vbKey4 52 4 键 
vbKey5 53 5 键 
vbKey6 54 6 键 
vbKey7 55 7 键 
vbKey8 56 8 键 
vbKey9 57 9 键 
下列常数代表数字键盘上的键:
常数 值 描述 
vbKeyNumpad0 0x60 0 键 
vbKeyNumpad1 0x61 1 键 
vbKeyNumpad2 0x62 2 键 
vbKeyNumpad3 0x63 3 键 
vbKeyNumpad4 0x64 4 键 
vbKeyNumpad5 0x65 5 键 
vbKeyNumpad6 0x66 6 键 
vbKeyNumpad7 0x67 7 键 
vbKeyNumpad8 0x68 8 键 
vbKeyNumpad9 0x69 9 键 
vbKeyMultiply 0x6A MULTIPLICATION SIGN (*) 键 
vbKeyAdd 0x6B PLUS SIGN (+) 键 
vbKeySeparator 0x6C ENTER 键 
vbKeySubtract 0x6D MINUS SIGN (-) 键 
vbKeyDecimal 0x6E DECIMAL POINT (.) 键 
vbKeyDivide 0x6F DIVISION SIGN (/) 键 
下列常数代表功能键:
常数 值 描述 
vbKeyF1 0x70 F1 键 
vbKeyF2 0x71 F2 键 
vbKeyF3 0x72 F3 键 
vbKeyF4 0x73 F4 键 
vbKeyF5 0x74 F5 键 
vbKeyF6 0x75 F6 键 
vbKeyF7 0x76 F7 键 
vbKeyF8 0x77 F8 键 
vbKeyF9 0x78 F9 键 
vbKeyF10 0x79 F10 键 
vbKeyF11 0x7A F11 键 
vbKeyF12 0x7B F12 键 
vbKeyF13 0x7C F13 键 
vbKeyF14 0x7D F14 键 
vbKeyF15 0x7E F15 键 
vbKeyF16 0x7F F16 键 
admin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: