ホットキーを使ってフォームをアクティブにする

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

 API関数のRegisterHotKeyおよびUnregisterHotKeyを使用してウィンドウハンドルに関連づけられたホットキーを設定・解除する事ができる。 ホットキーを指定する際は、そのキーと修飾キー(ALT, CTRL, SHIFT)及び、ホットキーのIDを指定する必要がある。 ここでは、ホットキーのIDをキーと修飾キーの値から決定している。
 また、実際にホットキーが押されたときには、WM_HOTKEYメッセージが送られてくるので、これに対してフォームをアクティブにする記述をすれば、ホットキーが押された時点でフォームをアクティブにすることができる。 なお、ホットキーを識別するためには、lParamの値が指定したホットキーと等しいかを比較する必要がある。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

    ' ホットキーを登録する
    <DllImport("user32", EntryPoint:="RegisterHotKey")> _
    Private Shared Function RegisterHotKey( _
        ByVal hWnd As IntPtr, _
        ByVal id As Integer, _
        ByVal fsModifier As Integer, _
        ByVal vk As Integer) _
    As Integer
    End Function

    ' ホットキーを解除する
    <DllImport("user32", EntryPoint:="UnregisterHotKey")> _
    Private Shared Function UnregisterHotKey( _
        ByVal hWnd As IntPtr, _
        ByVal id As Integer) _
    As Integer
    End Function

    ' ホットキーの修飾キー
    Private Const MOD_ALT As Byte = &H1
    Private Const MOD_CONTROL As Byte = &H2
    Private Const MOD_SHIFT As Byte = &H4

    <Flags()> _
    Private Enum KeyModifiers As Integer

        None = 0
        Alt = MOD_ALT
        Control = MOD_CONTROL
        Shift = MOD_SHIFT

    End Enum



    Dim id As Integer ' ホットキーのID
    Dim lParam As IntPtr ' WndProc()メソッドでホットキーを識別するためのlParam値

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Alt + Ctrl + Spaceをホットキーに指定する
        RegisterHotKey(Me.Handle, Keys.Space, KeyModifiers.Alt Or KeyModifiers.Control, id, lParam)

    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        ' 指定したホットキーを解除する
        UnregisterHotKey(Me.Handle, id)

    End Sub

    ' ホットキーの設定を行う
    Private Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal key As Keys, ByVal modifier As KeyModifiers, ByRef id As Integer, ByRef lParam As IntPtr) As Boolean

        ' これらの値は戻り値として返される
        id = CInt(key) Or CInt(modifier) * &H100
        lParam = New IntPtr(CInt(modifier) Or CInt(key) * &H10000)

        ' ホットキーの指定 
        Dim result As Integer

        result = RegisterHotKey(hWnd, id, CInt(modifier), CInt(key))

        Return (result <> 0)

    End Function

    ' ホットキーの入力メッセージを取得する
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        Const WM_HOTKEY As Integer = &H312

        If m.Msg = WM_HOTKEY AndAlso m.LParam.Equals(lParam) Then

            ' フォームをアクティブにする
            Me.Activate()

        Else

            ' 基底クラスでのメッセージ処理
            MyBase.WndProc(m)

        End If

    End Sub

#End Region

End Class