Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSG
hwnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function WaitMessage Lib "user32" () As Long
Private Declare Function PeekMessage Lib "user32" _
Alias "PeekMessageA" _
(ByRef lpMsg As MSG, ByVal hwnd As Long, _
ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long, _
ByVal wRemoveMsg As Long) As Long
Private Declare Function TranslateMessage Lib "user32" _
(ByRef lpMsg As MSG) As Long
Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Const WM_KEYDOWN As Long = &H100
Private Const PM_REMOVE As Long = &H1
Private Const PM_NOREMOVE As Long = &H0
Private Const WM_CHAR As Long = &H102
Private bExitLoop As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
[COLOR=seagreen]'intercept user keystrokes within column F:F[/COLOR]
If Union(Target, Columns("F:F")).Address = _
Columns("F:F").Address Then
StartKeyWatch
Else
StopKeyWatch
End If
End Sub
Private Sub StartKeyWatch()
Dim msgMessage As MSG
Dim bCancel As Boolean
Dim iKeyCode As Integer
Dim lXLhwnd As Long
[COLOR=seagreen]'handle the ESC key.[/COLOR]
On Error GoTo errHandler:
Application.EnableCancelKey = xlErrorHandler
[COLOR=seagreen] 'initialize this boolean flag.[/COLOR]
bExitLoop = False
[COLOR=seagreen]'get the app hwnd.[/COLOR]
lXLhwnd = FindWindow("XLMAIN", Application.Caption)
Do
WaitMessage
[COLOR=seagreen] 'check for a key press and remove it from the msg queue.[/COLOR]
If PeekMessage _
(msgMessage, lXLhwnd, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE) Then
[COLOR=seagreen] 'strore the virtual key code for later use.[/COLOR]
[COLOR=seagreen] [COLOR=black]iKeyCode = msgMessage.wParam[/COLOR]
'translate the virtual key code into a char msg.
[/COLOR] TranslateMessage msgMessage
PeekMessage msgMessage, lXLhwnd, WM_CHAR, _
WM_CHAR, PM_REMOVE
[COLOR=seagreen] 'for some obscure reason, the following[/COLOR]
[COLOR=seagreen] 'keys are not trapped inside the event handler[/COLOR]
[COLOR=seagreen]'so we handle them here.[/COLOR]
If iKeyCode = vbKeyBack Then SendKeys "{BS}"
If iKeyCode = vbKeyReturn Then SendKeys "{ENTER}"
[COLOR=seagreen]'assume the cancel argument is False.[/COLOR]
bCancel = False
[COLOR=seagreen]'the VBA RaiseEvent statement does not seem to return ByRef arguments[/COLOR]
[COLOR=seagreen]'so we call a KeyPress routine rather than a propper event handler.[/COLOR]
Sheet_KeyPress _
ByVal msgMessage.wParam, ByVal iKeyCode, ByVal ActiveCell, bCancel
[COLOR=seagreen]'if the key pressed is allowed post it to the application.[/COLOR]
If bCancel = False Then
PostMessage _
lXLhwnd, msgMessage.Message, msgMessage.wParam, 0
End If
End If
errHandler:
[COLOR=seagreen] 'allow the processing of other msgs.[/COLOR]
DoEvents
Loop Until bExitLoop
End Sub
Private Sub StopKeyWatch()
[COLOR=seagreen]'set this boolean flag to exit the above loop.[/COLOR]
bExitLoop = True
End Sub
Private Sub Sheet_KeyPress _
(ByVal KeyAscii As Integer, ByVal KeyCode As Integer, _
ByVal Target As Range, Cancel As Boolean)
[COLOR=seagreen]'which key has been pressed ?[/COLOR]
Select Case KeyCode
Case vbKeyTab, _
vbKeyReturn, _
vbKeyLeft, _
vbKeyUp, _
vbKeyRight, _
vbKeyDown, _
vbKeyDelete[COLOR=seagreen] 'if it is a navigation key do nothing.[/COLOR]
Case Else [COLOR=seagreen]'else,cancel the key stoke and _[/COLOR]
[COLOR=seagreen] 'run these lines instead.[/COLOR]
Cancel = True
With Target
.Value = "ü"
.HorizontalAlignment = xlCenter
.Font.Name = "Wingdings"
End With
SendKeys "{ENTER}"
End Select
End Sub