77 Author : Kike Pérez
88 Version : 1.7
99 Created : 10/05/2017
10- Modified : 18/01 /2018
10+ Modified : 07/03 /2018
1111
1212 This file is part of QuickLib: https://github.com/exilon/QuickLib
1313
@@ -80,6 +80,32 @@ TConsoleProperties = record
8080 end ;
8181
8282 TOutputProc<T> = reference to procedure(const aLine : T);
83+ TExecuteProc = reference to procedure;
84+
85+ TConsoleMenuOption = record
86+ private
87+ fCaption : string;
88+ fKey : Word;
89+ fOnKeyPressed : TExecuteProc;
90+ public
91+ property Caption : string read fCaption write fCaption;
92+ property Key : Word read fKey write fKey;
93+ property OnKeyPressed : TExecuteProc read fOnKeyPressed write fOnKeyPressed;
94+ procedure DoKeyPressed ;
95+ end ;
96+
97+ TConsoleMenu = class
98+ private
99+ fConsoleMenu : array of TConsoleMenuOption;
100+ fMenuColor : TConsoleColor;
101+ procedure WriteMenu ;
102+ public
103+ constructor Create;
104+ property MenuColor : TConsoleColor read fMenuColor write fMenuColor;
105+ procedure AddMenu (const cMenuCaption : string; const cMenuKey : Word; MenuAction : TExecuteProc); overload;
106+ procedure AddMenu (MenuOption : TConsoleMenuOption); overload;
107+ procedure WaitForKeys ;
108+ end ;
83109
84110 procedure cout (const cMsg : Integer; cEventType : TLogEventType); overload;
85111 procedure cout (const cMsg : Double; cEventType : TLogEventType); overload;
@@ -96,6 +122,7 @@ TConsoleProperties = record
96122 function ClearScreen : Boolean;
97123 procedure ClearLine ; overload;
98124 procedure ClearLine (Y : Integer); overload;
125+ procedure ProcessMessages ;
99126 procedure ConsoleWaitForEnterKey ;
100127 procedure RunConsoleCommand (const aCommand, aParameters : String; CallBack : TOutputProc<PAnsiChar> = nil ; OutputLines : TStrings = nil );
101128 procedure InitConsole ;
@@ -346,6 +373,42 @@ function ConsoleKeyPressed(ExpectedKey: Word): Boolean;
346373 end ;
347374end ;
348375
376+ function GetConsoleKeyPressed : Word;
377+ var
378+ lpNumberOfEvents: DWORD;
379+ lpBuffer: TInputRecord;
380+ lpNumberOfEventsRead : DWORD;
381+ nStdHandle: THandle;
382+ begin
383+ Result := 0 ;
384+ nStdHandle := GetStdHandle(STD_INPUT_HANDLE);
385+ lpNumberOfEvents := 0 ;
386+ GetNumberOfConsoleInputEvents(nStdHandle, lpNumberOfEvents);
387+ if lpNumberOfEvents <> 0 then
388+ begin
389+ PeekConsoleInput(nStdHandle, lpBuffer, 1 , lpNumberOfEventsRead);
390+ if lpNumberOfEventsRead <> 0 then
391+ begin
392+ if lpBuffer.EventType = KEY_EVENT then
393+ begin
394+ Result := lpBuffer.Event.KeyEvent.wVirtualKeyCode;
395+ FlushConsoleInputBuffer(nStdHandle);
396+ end
397+ else FlushConsoleInputBuffer(nStdHandle);
398+ end ;
399+ end ;
400+ end ;
401+
402+ procedure ProcessMessages ;
403+ var
404+ Msg: TMsg;
405+ begin
406+ while integer(PeekMessage(Msg, 0 , 0 , 0 , PM_REMOVE)) <> 0 do begin
407+ TranslateMessage(Msg);
408+ DispatchMessage(Msg);
409+ end ;
410+ end ;
411+
349412procedure ConsoleWaitForEnterKey ;
350413var
351414 msg: TMsg;
@@ -463,6 +526,123 @@ procedure InitConsole;
463526 LastMode := 3 ; // CO80;
464527end ;
465528
529+ { TConsoleMenu }
530+
531+ procedure TConsoleMenu.AddMenu (const cMenuCaption: string; const cMenuKey: Word; MenuAction: TExecuteProc);
532+ var
533+ conmenu : TConsoleMenuOption;
534+ begin
535+ conmenu.Caption := cMenuCaption;
536+ conmenu.Key := cMenuKey;
537+ conmenu.OnKeyPressed := MenuAction;
538+ fConsoleMenu := fConsoleMenu + [conmenu];
539+ end ;
540+
541+ procedure TConsoleMenu.AddMenu (MenuOption: TConsoleMenuOption);
542+ begin
543+ fConsoleMenu := fConsoleMenu + [MenuOption];
544+ end ;
545+
546+ constructor TConsoleMenu.Create;
547+ begin
548+ fMenuColor := ccLightCyan;
549+ end ;
550+
551+ procedure TConsoleMenu.WaitForKeys ;
552+ var
553+ msg: TMsg;
554+ conmenu : TConsoleMenuOption;
555+ keypressed : Word;
556+ begin
557+ WriteMenu;
558+ while True do
559+ begin
560+ // check key pressed
561+ keypressed := GetConsoleKeyPressed;
562+ for conmenu in fConsoleMenu do
563+ begin
564+ if keypressed = conmenu.Key then conmenu.DoKeyPressed;
565+ end ;
566+ if keypressed = VK_ESCAPE then
567+ begin
568+ coutXY(50 ,12 ,' Exiting...' ,etInfo);
569+ Exit;
570+ end ;
571+
572+ { $ifndef LVCL}
573+ if GetCurrentThreadID=MainThreadID then CheckSynchronize{ $ifdef WITHUXTHEME} (1000 ){ $endif} else
574+ { $endif}
575+ WaitMessage;
576+ while PeekMessage(msg,0 ,0 ,0 ,PM_REMOVE) do
577+ begin
578+ if Msg.Message = WM_QUIT then Exit
579+ else
580+ begin
581+ TranslateMessage(Msg);
582+ DispatchMessage(Msg);
583+ end ;
584+ end ;
585+ end ;
586+ end ;
587+
588+ function GetCharFromVirtualKey (Key: Word): string;
589+ var
590+ keyboardState: TKeyboardState;
591+ asciiResult: Integer;
592+ begin
593+ GetKeyboardState(keyboardState) ;
594+
595+ SetLength(Result, 2 ) ;
596+ asciiResult := ToAscii(key, MapVirtualKey(key, 0 ), keyboardState, @Result[1 ], 0 ) ;
597+ case asciiResult of
598+ 0 : Result := ' ' ;
599+ 1 : SetLength(Result, 1 ) ;
600+ 2 :;
601+ else
602+ Result := ' ' ;
603+ end ;
604+ end ;
605+
606+ procedure TConsoleMenu.WriteMenu ;
607+ var
608+ conmenu : TConsoleMenuOption;
609+ ckey : string;
610+ coord : TCoord;
611+ begin
612+ coord.X := 0 ;
613+ coord.Y := 0 ;
614+ SetCursorPos(coord);
615+ TextColor(fMenuColor);
616+ for conmenu in fConsoleMenu do
617+ begin
618+ case conmenu.Key of
619+ VK_F1 : ckey := ' F1' ;
620+ VK_F2 : ckey := ' F2' ;
621+ VK_F3 : ckey := ' F3' ;
622+ VK_F4 : ckey := ' F4' ;
623+ VK_F5 : ckey := ' F5' ;
624+ VK_F6 : ckey := ' F6' ;
625+ VK_F7 : ckey := ' F7' ;
626+ VK_F8 : ckey := ' F8' ;
627+ VK_F9 : ckey := ' F9' ;
628+ VK_F10 : ckey := ' F10' ;
629+ VK_F11 : ckey := ' F11' ;
630+ VK_F12 : ckey := ' F12' ;
631+ else ckey := GetCharFromVirtualKey(conmenu.Key);
632+ end ;
633+ Write(Format(' [%s] %s ' ,[ckey,conmenu.Caption]));
634+ end ;
635+ write(' [ESC] Exit' );
636+ TextColor(ccWhite);
637+ end ;
638+
639+ { TConsoleMenuOption }
640+
641+ procedure TConsoleMenuOption.DoKeyPressed ;
642+ begin
643+ if Assigned(fOnKeyPressed) then fOnKeyPressed;
644+ end ;
645+
466646initialization
467647InitializeCriticalSection(CSConsole);
468648// init stdout if not a service
0 commit comments