{$mode ObjFpc}
{Apptype gui}
uses windows;
Const MainWndClass='MainWindowClass';
      HotKeyId=$2;
      HotKeyControlId=$5;
      ButtonControlId=$6;
      Wm_ChangeHotKeys=Wm_User+2;
var  MainWnd,HotKeysWnd,ButtonWnd:hWnd;
     HotKeysFlags,HotKeysLetter:byte;//<--- changed
     buf:word;//<---changed
function MainProc(Wnd:hWnd; Msg:cardinal; wparam,lparam:longint):longint; stdcall;
begin
     result:=0;
     case Msg of
          Wm_Create:begin
                         HotKeysWnd:=CreateWindowEx(0,HotKey_Class,'Hot Keys',
                                                    Ws_Child or Ws_Visible,
                                                    2,5,
                                                    150,
                                                    20,
                                                    Wnd,
                                                    HotKeyControlId,
                                                    system.mainInstance,
                                                    nil);
                         ButtonWnd:=CreateWindowEx(0,'Button','Change',
                                                   Ws_Child or Ws_Visible,
                                                   2,35,
                                                   70,
                                                   20,
                                                   Wnd,
                                                   ButtonControlId,
                                                   system.mainInstance,
                                                   nil);
                         SendMessage(HotKeysWnd,HKM_SetHotKey,MakeWParam(HotKeysLetter,HotKeysFlags),0);
                   end;
          Wm_Destroy:begin
                          PostQuitMessage(0);
                     end;
          Wm_HotKey:case wParam of
                         HotKeyId: MessageBox(0,PChar('Main function'),PChar('Hot Keys!!!'),Mb_Ok);
                    end;
          Wm_Command: case LoWord(wParam) of
                           ButtonControlId:case HiWord(wParam) of
                                                Bn_Clicked:begin
                                                                buf:=SendMessage(HotKeysWnd,HKM_GetHotKey,0,0);
                                                                if buf=0 then writeln('Error');
                                                                writeln('Flags:  ',Hi(buf));
                                                                writeln('Letter: ',Lo(buf),' = ',chr(Lo(buf)));
                                                                SendMessage(Wnd,Wm_ChangeHotKeys,MakeWParam(buf,0),0);//<---changed
                                                           end;
                                           end;
                      end;
           Wm_ChangeHotKeys:begin
                                buf:=LoWord(wParam);//<---changed
                                writeln('Flags Received:  ',Hi(buf));
                                writeln('Letter Received: ',Lo(buf),' = ',chr(Lo(buf)));
                                if not(UnregisterHotKey(wnd,HotKeyId)) then
                                   writeln('Hot key unregistering error');
                                HotKeysFlags:=Hi(buf);
                                HotKeysLetter:=Lo(buf);
                                if not(RegisterHotKey(Wnd,HotKeyId,HotKeysFlags,HotKeysLetter)) then
                                   writeln('Cannot register new keys combination');
                                MessageBox(0,PChar('Receive'),PChar('Message was received'),Mb_Ok);
                           end;
           else result:=DefWindowProc(Wnd,Msg,wparam,lparam);
     end;
end;

function RegisterMainWndClass(ClassName:PChar):boolean;
var WndClass:PWndClassEx;
begin
 WndClass:=new(PWndClassEx);
  with WndClass^ do
       begin
            cbSize:=sizeof(WndClassEx);
            style := CS_DBLCLKS or CS_HREDRAW or
                     CS_OWNDC or CS_VREDRAW or Cs_ParentDc;
            LpfnWndProc:=@MainProc;
            cbClsExtra:=0;
            cbWndExtra:=0;
            hInstance:=System.HInstance;
            hIcon:=LoadIcon(0,Idi_Asterisk);
            hCursor:=LoadCursor(0,Idc_Arrow);
            hbrBackground:=Color_BtnFace+1;
            lpszMenuName:=nil;
            lpszClassName:=ClassName;
            hIconSm:=0;
       end;
  result:=RegisterClassEx(WndClass)<>0;
end;

function CreateMainWnd(ClassName:PChar):hWnd;
begin
     result:=CreateWindowEx(WS_EX_APPWINDOW,
             ClassName,
             '',
             Ws_Border or Ws_Caption or Ws_ClipCHildren or
             Ws_OverlappedWindow  or Ws_SizeBox or
             Ws_Visible,
             CW_USEDEFAULT, CW_USEDEFAULT,
             200, 130,
             0,
             0,
             HInstance,
             nil
             );
     HotKeysFlags:=Mod_Control;
     HotKeysLetter:=ord('L');
     writeln('Init Flags:  ',HotKeysFLags);
     writeln('Init Letter: ',HotKeysLetter);
     writeln;
     RegisterHotKey(result,HotKeyId,HotKeysFlags,HotKeysLetter);
end;

var Msg:TMsg;
begin
  RegisterMainWndClass(MainWndClass);
  MainWnd:=CreateMainWnd(MainWndClass);
  UpdateWindow(MainWnd);
  ShowWindow(MainWnd,Sw_ShowNa);
  while GetMessage(Msg,0,0,0) do begin
        TranslateMessage(Msg);
        DispatchMessage(Msg);
  end;
end.