タイトル 同時に押されている仮想キーコードを取得する
対象言語 VB4.0以降,Access95以降
動作確認OS Windows95,98,NT4.0
使用関数 GetKeyboardState
改築日 1999/08/12
Source Download

同時に押されている仮想キーコードを取得します。


例えば、[A]と[B]と[Alt] が同時に押されているかを判断する事が出来ます。

256Byteのバッファの配列にセットされ、該当するキーが押されていれば、そのキーのバイトの上位ビットに1、
離されていれば0がセットされます。
また、奇数回押された場合は下位ビットに1、偶数回押された場合は0がセットされます。


1. フォームを作成し、テキストボックス(Text1) を貼り付けてください。


Option Explicit

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
  Dim buf() As Byte
  Dim longret As Long
  Dim i As Integer

  Text1 = vbNullString
  buf = String$(256, Chr$(0))
  '現在押されている仮想キーコードを取得する
  longret = GetKeyboardState(buf(0))

  For i = 0 To 255
    If buf(i) And &H80 Then
      Text1 = Text1 & vbCrLf & Hex$(i)
    End If
  Next

End Sub


Private Sub Text1_KeyPress(KeyAscii As Integer)
  KeyAscii = 0
End Sub



2.モジュールウインドウを作成し、下のソースを入力してください。

Option Explicit

'キーボードの状態を取得し、仮想キーコードを取得する
Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long

Public Const VK_LBUTTON = &H1
Public Const VK_RBUTTON = &H2
Public Const VK_CANCEL = &H3
Public Const VK_MBUTTON = &H4

Public Const VK_BACK = &H8 'Backspace
Public Const VK_TAB = &H9

Public Const VK_CLEAR = &HC
Public Const VK_RETURN = &HD

Public Const VK_SHIFT = &H10
Public Const VK_CONTROL = &H11
Public Const VK_MENU = &H12 'Alt
Public Const VK_PAUSE = &H13
Public Const VK_CAPITAL = &H14 'CapsLock

Public Const VK_ESCAPE = &H1B

Public Const VK_SPACE = &H20
Public Const VK_PRIOR = &H21 'PageUp
Public Const VK_NEXT = &H22 'PageDown
Public Const VK_END = &H23
Public Const VK_HOME = &H24
Public Const VK_LEFT = &H25
Public Const VK_UP = &H26
Public Const VK_RIGHT = &H27
Public Const VK_DOWN = &H28
Public Const VK_SELECT = &H29
Public Const VK_PRINT = &H2A
Public Const VK_EXECUTE = &H2B
Public Const VK_SNAPSHOT = &H2C 'PintScreen
Public Const VK_INSERT = &H2D
Public Const VK_DELETE = &H2E
Public Const VK_HELP = &H2F

Public Const VK_0 = &H30
Public Const VK_1 = &H31
Public Const VK_2 = &H32
Public Const VK_3 = &H33
Public Const VK_4 = &H34
Public Const VK_5 = &H35
Public Const VK_6 = &H36
Public Const VK_7 = &H37
Public Const VK_8 = &H38
Public Const VK_9 = &H39

Public Const VK_A = &H41
Public Const VK_B = &H42
Public Const VK_C = &H43
Public Const VK_D = &H44
Public Const VK_E = &H45
Public Const VK_F = &H46
Public Const VK_G = &H47
Public Const VK_H = &H48
Public Const VK_I = &H49
Public Const VK_J = &H4A
Public Const VK_K = &H4B
Public Const VK_L = &H4C
Public Const VK_M = &H4D
Public Const VK_N = &H4E
Public Const VK_O = &H4F
Public Const VK_P = &H50
Public Const VK_Q = &H51
Public Const VK_R = &H52
Public Const VK_S = &H53
Public Const VK_T = &H54
Public Const VK_U = &H55
Public Const VK_V = &H56
Public Const VK_W = &H57
Public Const VK_X = &H58
Public Const VK_Y = &H59
Public Const VK_Z = &H5A

Public Const VK_NUMPAD0 = &H60 'Numkey
Public Const VK_NUMPAD1 = &H61
Public Const VK_NUMPAD2 = &H62
Public Const VK_NUMPAD3 = &H63
Public Const VK_NUMPAD4 = &H64
Public Const VK_NUMPAD5 = &H65
Public Const VK_NUMPAD6 = &H66
Public Const VK_NUMPAD7 = &H67
Public Const VK_NUMPAD8 = &H68
Public Const VK_NUMPAD9 = &H69
Public Const VK_MULTIPLY = &H6A '*
Public Const VK_ADD = &H6B '+
Public Const VK_SEPARATOR = &H6C ',
Public Const VK_SUBTRACT = &H6D '-
Public Const VK_DECIMAL = &H6E '.
Public Const VK_DIVIDE = &H6F '/
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87

Public Const VK_NUMLOCK = &H90
Public Const VK_SCROLL = &H91

'以下GetAsyncKeyState ,GetKeyState APIのみ
Public Const VK_LSHIFT = &HA0
Public Const VK_RSHIFT = &HA1
Public Const VK_LCONTROL = &HA2
Public Const VK_RCONTROL = &HA3
Public Const VK_LMENU = &HA4
Public Const VK_RMENU = &HA5

Public Const VK_ATTN = &HF6
Public Const VK_CRSEL = &HF7
Public Const VK_EXSEL = &HF8
Public Const VK_EREOF = &HF9
Public Const VK_PLAY = &HFA
Public Const VK_ZOOM = &HFB
Public Const VK_NONAME = &HFC
Public Const VK_PA1 = &HFD
Public Const VK_OEM_CLEAR = &HFE





Copyright (C)1997-2001 空耳工房 MY2Project All rights reserved.