HOME > エクセルVBA > サンプル > keybd_event

keybd_event


【ファイル名】keybd_event.xlsm
【バージョン】Ver 1.0.0.0
【作成日】2021年05月16日
【動作確認】Windows 10
【必要なソフト】Microsoft Excel 2010
【ダウンロード】keybd_event.zip
【説明】Windows APIのkeybd_event関数でキーを打鍵します。SendKeys で使えないWindows、NUM LOCKキーも使えます。
画面

【外部リンク】Windows > Apps > Win32 > API > Keyboard and Mouse Input > Winuser.h >  keybd_eventGetKeyboardState
【外部リンク】Windows > Apps > Win32 > Desktop Technologies > Desktop App User Interface > User Interaction > Legacy Features > Keyboard and Mouse Input >  Virtual-Key Codes
キーの定義は、VBAのKeyCodeConstantsが使えると思います。
【外部リンク】Docs > Office VBA リファレンス > 言語リファレンス > リファレンス > 定数 >  KEYCODE
【外部リンク】Docs > Office VBA リファレンス > 言語リファレンス > リファレンス > 財務諸表 >  SendKeys

customUI14.xml
<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="onLoad">
  <ribbon startFromScratch="false">
    <tabs>
      <tab id="Tab1" label="サンプル" insertBeforeMso="TabHome" keytip="S">
        <group id="Group1" label="keybd_event">
          <box id="Box1" boxStyle="vertical">
              <button id="ActionCenterButton" label="アクションセンターを表示" keytip="A" imageMso="HappyFace"
                      supertip="アクションセンターを表示します。" onAction="onAction" />
              <button id="DesktopButton" label="デスクトップを表示" keytip="D" imageMso="HappyFace"
                      supertip="デスクトップを表示します。" onAction="onAction" />
              <button id="ExplorerButton" label="エクスプローラーを起動" keytip="E" imageMso="Folder"
                      supertip="エクスプローラーを起動します。" onAction="onAction" />
              <button id="SettingsButton" label="Windows の設定" imageMso="HappyFace" keytip="I"
                      supertip="Windows の設定を起動します。" onAction="onAction" />
              <button id="FileRunButton" label="ファイル名を指定して実行" keytip="R" imageMso="HappyFace"
                      supertip="「ファイル名を指定して実行」を起動します。" onAction="onAction" />
          </box>
          <box id="Box2" boxStyle="vertical">
              <button id="ScreenCaptureButton" label="画面をキャプチャー" imageMso="Camera" keytip="S"
                      supertip="画面をキャプチャーします。" onAction="onAction" />
              <button id="WindowCaptureButton" label="カレント ウィンドウをキャプチャー" imageMso="Camera" keytip="W"
                      supertip="カレント ウィンドウをキャプチャーします。" onAction="onAction" />
          </box>
          <box id="Box3" boxStyle="vertical">
              <button id="NUMLOCKButton" label="NUM LOCKキー" imageMso="Heart" keytip="N"
                       supertip="NUM LOCKキーを押します。" onAction="onAction" />
              <button id="CAPSButton" label="CAPS LOCKキー" imageMso="Heart" keytip="C"
                      supertip="CAPS LOCKキーを押します。" onAction="onAction" />
              <button id="SCROLLButton" label="SCROLL LOCKキー" imageMso="Heart" keytip="O"
                      supertip="SCROLL LOCKキーを押します。" onAction="onAction" />
          </box>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>
このページの上

Module1
Option Explicit

' keybd_event 関数は、SendKeys で使えないWindows、NUM LOCK キーなども使えます。

Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2

#If Win64 Then
  ' https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-keybd_event
  Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)

  ' https://docs.microsoft.com/ja-jp/windows/win32/api/winuser/nf-winuser-getkeyboardstate
  Private Declare PtrSafe Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long

#Else
  Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
  Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
#End If

Private rbRibbonUI As IRibbonUI   ' リボン

Sub onLoad(ribbon As IRibbonUI) ' リボンの初期処理
  Set rbRibbonUI = ribbon ' リボンの表示を更新できるようにするためにリボンをセットする
  rbRibbonUI.Invalidate ' リボンの表示を更新します。
End Sub

Sub onAction(control As IRibbonControl) ' 各メニューの処理
  On Error GoTo ErrorHandler ' エラー処理ルーチン
  
  Select Case control.ID
    Case "ActionCenterButton"
      WindowsAKey
    Case "DesktopButton"
      WindowsDKey
    Case "ExplorerButton"
      WindowsEKey
    Case "SettingsButton"
      WindowsIKey
    Case "FileRunButton"
      WindowsRKey
    Case "ScreenCaptureButton"
      PrintScreenKey
    Case "WindowCaptureButton"
      PrintScreenCtrlKey
    Case "NUMLOCKButton"
      PressNumLock
    Case "CAPSButton"
      PressCapsLock
    Case "SCROLLButton"
      PressScrollLock
    Case Else
      Beep ' 警告音
      MsgBox Prompt:=control.ID & "の処理がありません!", Buttons:=vbCritical + vbSystemModal, Title:="エラー"
      Exit Sub
  End Select
  
  Exit Sub
'
ErrorHandler: ' エラー処理ルーチン
  Beep ' 警告音
  MsgBox Prompt:="エラー番号:" & Err.Number & vbCrLf & Err.Description, Buttons:=vbCritical + vbSystemModal, Title:="エラー"
End Sub

' アクションセンターを表示する
Sub WindowsAKey()
  keybd_event VK_LWIN, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyA, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyA, 0, KEYEVENTF_KEYUP, 0
  DoEvents
  keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
End Sub

' デスクトップを表示する
Sub WindowsDKey()
  keybd_event VK_LWIN, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyD, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyD, 0, KEYEVENTF_KEYUP, 0
  DoEvents
  keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
End Sub

' エクスプローラーを起動する
Sub WindowsEKey()
  keybd_event VK_LWIN, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyE, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyE, 0, KEYEVENTF_KEYUP, 0
  DoEvents
  keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
End Sub

' Windowsの設定
Sub WindowsIKey()
  keybd_event VK_LWIN, 0, 0, 0
  keybd_event VBA.KeyCodeConstants.vbKeyI, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyI, 0, KEYEVENTF_KEYUP, 0
  DoEvents
  keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
End Sub

' ファイル名を指定して実行
Sub WindowsRKey()
  keybd_event VK_LWIN, 0, 0, 0
  keybd_event VBA.KeyCodeConstants.vbKeyR, 0, 0, 0
  DoEvents
  keybd_event VBA.KeyCodeConstants.vbKeyR, 0, KEYEVENTF_KEYUP, 0
  DoEvents
  keybd_event VK_LWIN, 0, KEYEVENTF_KEYUP, 0
End Sub

' 画面のキャプチャー
Sub PrintScreenKey()
  keybd_event VK_SNAPSHOT, 0, 0, 0
  DoEvents
  keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
End Sub

' カレントウィンドウのキャプチャー
Sub PrintScreenCtrlKey()
  keybd_event VK_SNAPSHOT, 1, 0, 0
  DoEvents
  keybd_event VK_SNAPSHOT, 1, KEYEVENTF_KEYUP, 0
End Sub

' NUM LOCK、CAPS LOCK、SCROLL LOCKキーを押す
Sub PressLockKey(Optional Key As Byte = VK_NUMLOCK, Optional bState As Boolean = True)
  Dim keyState(0 To 255) As Byte
  
  GetKeyboardState keyState(0)
  
  If bState And Not (keyState(Key) And 1) Or _
  Not bState And (keyState(Key) And 1) Then
    keybd_event Key, &H45, KEYEVENTF_EXTENDEDKEY Or 0, 0
    DoEvents
    keybd_event Key, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
  End If

End Sub

' NUM LOCK
Sub PressNumLock()
  PressLockKey VK_NUMLOCK
End Sub

' CAPS LOCK
Sub PressCapsLock()
  PressLockKey VK_CAPITAL
End Sub

' SCROLL LOCK
Sub PressScrollLock()
  PressLockKey VK_SCROLL
End Sub
このページの上

KeysModule
Option Explicit

' https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
' WinUser.h

' 自分で定義しないで、VBA.KeyCodeConstants を使うのも良いかもしれません?
' VBA.KeyCodeConstants のキーコードの定数は下記のマイクロソフトのサイトを参照してください。
' https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/keycode-constants
' Docs Office VBA > リファレンス > 言語リファレンス > リファレンス > 定数 > KEYCODE

Public Const VK_LBUTTON = &H1
Public Const VK_RBUTTON = &H2
Public Const VK_CANCEL = &H3
Public Const VK_MBUTTON = &H4
Public Const VK_XBUTTON1 = &H5
Public Const VK_XBUTTON2 = &H6
Public Const VK_BACK = &H8
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
Public Const VK_PAUSE = &H13
Public Const VK_CAPITAL = &H14
Public Const VK_KANA = &H15
Public Const VK_HANGEUL = &H15
Public Const VK_HANGUL = &H15
Public Const VK_IME_ON = &H16
Public Const VK_JUNJA = &H17
Public Const VK_FINAL = &H18
Public Const VK_HANJA = &H19
Public Const VK_KANJI = &H19
Public Const VK_IME_OFF = &H1A
Public Const VK_ESCAPE = &H1B
Public Const VK_CONVERT = &H1C
Public Const VK_NONCONVERT = &H1D
Public Const VK_ACCEPT = &H1E
Public Const VK_MODECHANGE = &H1F
Public Const VK_SPACE = &H20
Public Const VK_PRIOR = &H21
Public Const VK_NEXT = &H22
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
Public Const VK_INSERT = &H2D
Public Const VK_DELETE = &H2E
Public Const VK_HELP = &H2F
Public Const VK_LWIN = &H5B
Public Const VK_RWIN = &H5C
Public Const VK_APPS = &H5D
Public Const VK_SLEEP = &H5F
Public Const VK_NUMPAD0 = &H60
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_NAVIGATION_VIEW = &H88
Public Const VK_NAVIGATION_MENU = &H89
Public Const VK_NAVIGATION_UP = &H8A
Public Const VK_NAVIGATION_DOWN = &H8B
Public Const VK_NAVIGATION_LEFT = &H8C
Public Const VK_NAVIGATION_RIGHT = &H8D
Public Const VK_NAVIGATION_ACCEPT = &H8E
Public Const VK_NAVIGATION_CANCEL = &H8F
Public Const VK_NUMLOCK = &H90
Public Const VK_SCROLL = &H91
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_BROWSER_BACK = &HA6
Public Const VK_BROWSER_FORWARD = &HA7
Public Const VK_BROWSER_REFRESH = &HA8
Public Const VK_BROWSER_STOP = &HA9
Public Const VK_BROWSER_SEARCH = &HAA
Public Const VK_BROWSER_FAVORITES = &HAB
Public Const VK_BROWSER_HOME = &HAC
Public Const VK_VOLUME_MUTE = &HAD
Public Const VK_VOLUME_DOWN = &HAE
Public Const VK_VOLUME_UP = &HAF
Public Const VK_MEDIA_NEXT_TRACK = &HB0
Public Const VK_MEDIA_PREV_TRACK = &HB1
Public Const VK_MEDIA_STOP = &HB2
Public Const VK_MEDIA_PLAY_PAUSE = &HB3
Public Const VK_LAUNCH_MAIL = &HB4
Public Const VK_LAUNCH_MEDIA_SELECT = &HB5
Public Const VK_LAUNCH_APP1 = &HB6
Public Const VK_LAUNCH_APP2 = &HB7
このページの上