# RegisterMessage
{: .no_toc }

Subscribes a callback to a single Windows message type for a chosen window and descendant scope.

Syntax: *hook*.**RegisterMessage** *ParentHWND* **,** *Mode* **,** *MessageType* **,** *Callback*

*hook*
: *required* An [**IGetMessageHook**](/tB/Modules/HiddenModule/index.md#igetmessagehook-interface) instance, typically returned by [**RuntimeCreateGetMessageHook**](/tB/Modules/HiddenModule/RuntimeCreateGetMessageHook.md).

*ParentHWND*
: *required* **LongPtr**. The window the subscription anchors on.

*Mode*
: *required* [**EnumDescendantsModeFlags**](/tB/Modules/HiddenModule/index.md#enumdescendantsmodeflags). The descendant scope: only the exact window, all descendants, or only direct children.

*MessageType*
: *required* **Integer**. The Windows `WM_*` message identifier to subscribe to. Each call subscribes to one type; call **RegisterMessage** repeatedly to listen for several.

*Callback*
: *required* [**GetMessageHookHelper.GetMessageHandler**](/tB/Modules/HiddenModule/index.md#getmessagehandler). The function that receives matching messages. Pass `AddressOf` a function with the matching signature.

The subscription is recorded but does not start firing until [**Start**](/tB/Modules/HiddenModule/Start.md) is called on the hook. Existing subscriptions are not disturbed by adding new ones.

**RegisterMessage** checks none of its arguments: a window that does not exist, a *Mode* that is not one of the three values, and the same subscription given twice are accepted.

### Common Errors

| Error | When |
|---|---|
| [94 (`&H5E`)](/Reference/Run-time-Errors.md#94), *Invalid use of Null* | *ParentHWND*, *Mode* or *MessageType* is **Null** |

### Example

```tb check_build inherits=Form
Const WM_KEYDOWN = &H100
Const WM_CHAR = &H102

Sub HookKeyboard(ByVal h As IGetMessageHook)
    h.RegisterMessage Me.hWnd, AllDescendants, WM_KEYDOWN, AddressOf OnKeyDown
    h.RegisterMessage Me.hWnd, AllDescendants, WM_CHAR,    AddressOf OnChar
    h.Start
End Sub
```

### See Also

- [Start](/tB/Modules/HiddenModule/Start.md) method
- [Stop](/tB/Modules/HiddenModule/Stop.md) method
- [IGetMessageHook interface](/tB/Modules/HiddenModule/index.md#igetmessagehook-interface)
- [GetMessageHookHelper module](/tB/Modules/HiddenModule/index.md#getmessagehookhelper-module)
