Adding Keyboard Layouts for other languages

From NSIS Wiki
Jump to navigationJump to search
Author: CancerFace (talk, contrib)


NSIS forum thread started by user huq.duote.com


Description

These macros will allow you to add a keyboard layout and activate it for the current thread on the fly, or to unload an already loaded keyboard layout. The second macro will try to unload a keyboard layout and if it fails it will try to activate the next available layout first.

Usage

Call the macro passing the language ID that you want to load. For a list of Language IDs check this MSDN page (Identifier list).

To load a keyboard layout use:

!insertmacro LoadLangLayout <your_layout_ID>

To unload a keyboard layout use:

!insertmacro UnloadLangLayout <your_layout_ID>

For example to load the French keyboard layout use:

!insertmacro LoadLangLayout 0x040c

Macros

!macro LoadLangLayout LangID
!define Index "Line${__LINE__}"
  StrCpy $8 "${LangID}" "" 2
  StrCpy $8 "0000$8"
  System::Alloc ${NSIS_MAX_STRLEN}
  Pop $R0
  System::Call 'user32::GetKeyboardLayoutList(i ${NSIS_MAX_STRLEN}, i R0)i.r0'
  StrCpy $9 1
 
  loop-${Index}:
    System::Call '*$R0(i .r1)'
      IntFmt $2 "%08x" $1
      StrCpy $3 $2 "" -4
      StrCpy $4 "0x$3"
      IntOp $R0 $R0 + 4
    StrCmp $4 ${LangID} macro_end_${Index}
    StrCmp $9 $0 loop_end-${Index}
    IntOp $9 $9 + 1
    Goto loop-${Index}
 
  loop_end-${Index}:
    System::Call 'user32::LoadKeyboardLayoutA(t r8, i 1)i.r0'
 
  macro_end_${Index}:
    System::Call 'user32::ActivateKeyboardLayout(i ${LangID}, i 8)i.r0'
!undef Index
!macroend
!macro UnloadLangLayout LangID
!define Index "Line${__LINE__}"
  System::Call 'user32::UnloadKeyboardLayout(i ${LangID})i.r0'
  StrCmp $0 0 failed-${Index} end-${Index}
 
  failed-${Index}:
   System::Call 'user32::ActivateKeyboardLayout(i 1, i 8)i.r0'
   System::Call 'user32::UnloadKeyboardLayout(i ${LangID})i.r0'
 
  end-${Index}:
!undef Index
!macroend

Resources and Links


API Functions used: