Reputation: 314
I'm trying to figure out how to get my NSIS installer to respond to key presses. I thought I would get my answer here, but the link referenced in the answer isn't working.
This is what I've tried:
!include WinMessages.nsh
Section
DetailPrint "Starting"
SendMessage $HWNDPARENT ${WM_SETHOTKEY} 1616 0
Sleep 3000
SendMessage $HWNDPARENT ${WM_GETHOTKEY} 0 0 $R0
Pop $R1
StrCpy $R2 "$$R0 = $R0"
StrCpy $R2 "$R2$\n$$R1 = $R1"
IntCmp $R1 0 not_pressed pressed
pressed:
StrCpy $R2 "$R2$\nPressed"
Goto end
not_pressed:
StrCpy $R2 "$R2$\nNot pressed"
Goto end
end:
MessageBox MB_OK $R2
SectionEnd
I tried running the installer and pressing CTRL+ALT+P and various points, but I never get anything from the stack. I tried setting the hotkey to ${HOTKEYF_ALT}
, but that didn't make a difference (eventually I'd like to map to CTRL+ALT+F12, but I just want a key combo that works for now).
Edit:
I also tried replacing Sleep 3000
with
StrCpy $R3 0
DetailPrint "$$R3 = $R3"
IntOp $R3 $R3 + 1
IntCmp $R3 3000 0 -2
just in case that line was causing a problem. It didn't help.
Upvotes: 0
Views: 99
Reputation: 101756
The page on the forum you linked to used a custom version of the nsDialogs plug-in, it does not support this by default.
NSIS does not have support for global hot-keys because they are not common in installers.
A global hot-key to focus the installer window is possible but not really that useful:
!include WinMessages.nsh
Function .onGuiInit
!define /IfNDef HOTKEYF_CONTROL 2
!define /IfNDef HOTKEYF_ALT 4
!define /IfNDef VK_F12 0x7b
IntOp $0 ${HOTKEYF_CONTROL} | ${HOTKEYF_ALT}
IntOp $0 $0 << 8
IntOp $0 $0 | ${VK_F12}
SendMessage $hWndParent ${WM_SETHOTKEY} $0 0
FunctionEnd
The NsKeyHook plug-in can handle the WM_CHAR and WM_KEYUP messages so you will be able to perform custom actions when certain keys are pressed but this will only work when a window you have hooked in the installer has focus.
If you don't mind gross hacks, I invented a way to have a global hot-key on a custom page:
!include WinMessages.nsh
!include nsDialogs.nsh
Function onNotifyHack
Pop $0 ; HWND
Pop $1 ; Code
Pop $2 ; pNMH
!define /IfNDef NM_FIRST 0
!define /IfNDef /Math NM_SETFOCUS ${NM_FIRST} - 7
${If} $1 = ${NM_SETFOCUS}
System::Call 'USER32::SetForegroundWindow(p$hWndParent)'
MessageBox MB_OK "Hotkey pressed!"
${EndIf}
FunctionEnd
Function myPage
nsDialogs::Create 1018
Pop $0
${NSD_CreateLabel} 0 0 100% 12u "Press Ctrl+Alt+F12"
Pop $0
nsDialogs::CreateControl "SysListView32" ${WS_VISIBLE}|${WS_CHILD} ${WS_EX_TOOLWINDOW} 0 0 0 0 ""
Pop $1
System::Call 'USER32::SetParent(p$1,p0)'
${NSD_RemoveStyle} $1 ${WS_CHILD}
${NSD_OnNotify} $1 onNotifyHack
SendMessage $1 ${WM_SETHOTKEY} 0x067b 0
Push $1
nsDialogs::Show
Pop $1
SendMessage $1 ${WM_CLOSE} "" ""
FunctionEnd
Page Custom myPage
Page InstFiles
Section
SectionEnd
Upvotes: 1