[Solved] Read arrow key lazarus / 62-General ---------------------------------------------------------------------- NaolShow · 2021-11-11 12:40 Hi everyone, I am making an ASCII game, and I would like to let the users navigate into a list with the arrow keys (It's a list of Inventory Items). I found that there is the ReadKey function from CRT. It works great, I can just do ord(ReadKey()) and check if it equals 80 (down) or (72) up... The problem is that CRT is not compatible with UTF8 ? (https://stackoverflow.com/questions/44107137/console-cp-cant-be- change-when-i-use-unit-crt-in-pascal) And I can't find a way to just read the key and take back UTF8... So I've searched from the WinAPI (Windows unit) side, and found this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ReadKey: Char;var  Mode: DWORD = 0;begin  if GetConsoleMode(TextRec(Input).Handle, Mode) then    SetConsoleMode(TextRec(Input).Handle, 0);  Read(Result);  if Mode <> 0 then    SetConsoleMode(TextRec(Input).Handle, Mode);end; It works, but only with basic keys, like "abcdef...", "&é"'(-è_çà" but not with the arrow keys.. Is there a way to capture the arrow keys ? If not I'll just use WASD keys but it's just to be sure. Thanks everyone! ---------------------------------------------------------------------- Handoko · 2021-11-11 16:41 Quote from: NaolShow on November 11, 2021, 01:40:19 pmI am making an ASCII game ... You should set your aim high. Making graphics even 3D games using Lazarus/FPC is much easier than you think. ReadKey and also CRT unit are good for console programs and maybe classic turn based games. Don't use it if you want to make good games. Do you use Lazarus? Or FPC only? Lazarus is preferable if you want to learn and make games easier. Lazarus default installation has a good example for very beginners to learn making games: Lazarus main menu > Tools > Example Projects > in the search box, type: sprite Then you will get the spriteexample project. It shows you how to do these: - Loading image (in PlayGroundFormCreate) - Using image buffer (in UpdateImage) - Using KeyDown event (in PictureControlKeyDown) - Animating a sprite  (in UpdateImage) - Making sprite following mouse pointer (in UpdateImage) - Using GetKeyState (in UpdateImage) - Doing inertia movement (in UpdateImage) - Using a TTimer as main loop (which usually used for games) All the things above can be done using Lazarus in just about 300 LOC. Not so easy if you use FPC only. For you information, to prevent unintentional modifying the original source code and some user access issues (on Linux), you should not use the code directly from there. It is better to make a copy (for example to desktop) and use the copied version. And you should visit here: https://wiki.freepascal.org/Portal:HowTo_Demos That page has a list of short demos which are beginner-friendly. For game related topic, there you can find: - Load and show image from Resource Stream - Load a picture and show it - Moving an Object by pressing a key - Pausing and Continuing an animation - Control an Object's direction using the keyboard - Timer demo - A very simple single player Pong game - Game Map Editor - Simple animation - Moving circles - Sticky Balls - Rock, Paper, Scissors - Bullet and asteroid collision detection - Move a shape with a mouse - Sprite picking and moving - Dancing Balls - How to load a TImagelist at run time - Game Client and Server ---------------------------------------------------------------------- winni · 2021-11-11 16:42 Hi! Back to konsole games ... That' s the way it works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...     key := readkey;     if  key=#0 begin        key:=readkey;      case key of      #72 :  writeln('up');        #80 :  writeln('down');      #75 :  writeln('left');      #77:  writeln('right');      end; // caseend;  // if #0...  Winni ---------------------------------------------------------------------- NaolShow · 2021-11-11 16:54 Quote from: Handoko on November 11, 2021, 05:41:08 pmQuote from: NaolShow on November 11, 2021, 01:40:19 pmI am making an ASCII game ... You should set your aim high. Making graphics even 3D games using Lazarus/FPC is much easier than you think. ReadKey is good for console programs and maybe classic turn based games. Don't use it if you want to make good games. Do you use Lazarus? Or FPC only? Lazarus is preferable if you want to learn and make games easier. Lazarus default installation has a good example for very beginners to learn making games: Lazarus main menu > Tools > Example Projects > in the search box, type: sprite Then you will get the spriteexample project. It shows you how to do these: - Loading image (in PlayGroundFormCreate) - Using image buffer (in UpdateImage) - Using KeyDown event (in PictureControlKeyDown) - Animating a sprite  (in UpdateImage) - Making sprite following mouse pointer (in UpdateImage) - Using GetKeyState (in UpdateImage) - Doing inertia movement (in UpdateImage) - Using a TTimer as main loop (which usually used for games) All the things above can be done using Lazarus in just about 300 LOC. Not so easy if you use FPC only. For you information, to prevent unintentional modifying the original source code and some user access issues (on Linux), you should not use the code directly from there. It is better to make a copy (for example to desktop) and use the copied version. And you should visit here: https://wiki.freepascal.org/Portal:HowTo_Demos That page has a list of short demos which are beginner-friendly. For game related topic, there you can find: - Load and show image from Resource Stream - Load a picture and show it - Moving an Object by pressing a key - Pausing and Continuing an animation - Control an Object's direction using the keyboard - Timer demo - A very simple single player Pong game - Game Map Editor - Simple animation - Moving circles - Sticky Balls - Rock, Paper, Scissors - Bullet and asteroid collision detection - Move a shape with a mouse - Sprite picking and moving - Dancing Balls - How to load a TImagelist at run time - Game Client and Server Hi! Thanks for the reply That's the problem... I am making an ASCII game (so a console application) for a School Project and it is written in the specifications that we have to make an ASCII game. It's basically a turn by turn game in which we can fight against monsters, drop objects and then use them to create weapons, armors. (That's why I asked few questions regarding Inventories on the forum  ) And yes I am using LAZARUS, but I don't think I can import specific "libraries". And I've checked your links and in all of them is requires some libraries like: - "LCLType", "ExtCtrls" and it do not look like I have access to these in a console app But yes, if I could I would prefer doing a better game with 3D ---------------------------------------------------------------------- NaolShow · 2021-11-11 16:55 Quote from: winni on November 11, 2021, 05:42:30 pmHi! Back to konsole games ... That' s the way it works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...     key := readkey;     if  key=#0 begin        key:=readkey;      case key of      #72 :  writeln('up');        #80 :  writeln('down');      #75 :  writeln('left');      #77:  writeln('right');      end; // caseend;  // if #0...  Winni Hi! Thanks for the reply  ! Sadly it does not look like it is supported by default and I must use the Crt unit. Which as I said breaks the UTF 8 encoding of the console... Do you know any other solution ?  Thanks! ---------------------------------------------------------------------- Handoko · 2021-11-11 17:13 I'm not familiar with CRT unit and I'm not a Windows user, not much I can help. But you can try this: https://forum.lazarus.freepascal.org/index.php/topic,39750.msg273783.h tml ---------------------------------------------------------------------- winni · 2021-11-11 17:20 Hi! Figure out what your konsole returns for every key: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program getord; var s : string;    i : integer;beginrepeatreadln (s);for i := 1 to length(s) do     writeln (ord(s[i]));until s = '';end.  For example for cursor up it returns: 27 91 65 Winni ---------------------------------------------------------------------- NaolShow · 2021-11-11 17:29 Quote from: Handoko on November 11, 2021, 06:13:48 pmI'm not familiar with CRT unit and I'm not a Windows user, not much I can help. But you can try this: https://forum.lazarus.freepascal.org/index.php/topic,39750.msg273783.h tml Hi! So I've tried with this code from the post: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function KeyInput: Char;   Var    Key: TKeyEvent;  Begin   InitKeyBoard;    Try     Key:= GetKeyEvent;     writeln('1: ', Key);     Key:= TranslateKeyEvent(Key);     writeln('2: ', Key);     Result:= GetKeyEventChar(Key);     writeln('3: ', Result);     Finally     DoneKeyBoard;    End;  End; And it works with every keys up to the 2nd step. But the arrow keys are just set to 0 on the third step (so with the GetKeyEventChar function) So I'll just modify the function to return the 2nd step (TranslateKeyEvent) and just use this to check if it's an arrow key press. I'll just check if the returned value do not change across systems and it will be good. Thanks again guys!  EDIT: Even better with this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      Key:= GetKeyEvent;     writeln('1: ', Key);     Key := TranslateKeyEvent(Key);     writeln('2: ', Key);     writeln('3: ',  KeyEventToString(Key));     It gives me directly "Right", "Down", "Up" or "Left" when pressing the arrow keys! Thanks again!! ---------------------------------------------------------------------- BobDog · 2021-11-11 18:45 The c runtime will get the arrow keys. external 'msvcrt' in windows external 'c' in linux (I think) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     function getch():byte; cdecl external 'msvcrt.dll' name '_getch';     function getkey():char;  var  b:byte;  begin  b:=getch();  if (b=224) then exit(chr(0));   exit(chr(b));  end;    var  key:char;      begin  writeln('Press some arrow keys, or esc to finish.');  while(true) do  begin  key:=getkey();  if (key=chr(27)) then halt;  if (key)='H' then writeln('    UP');  if (key)='P' then writeln('    DOWN');  if (key)='M' then writeln('    RIGHT');  if (key)='K' then writeln('    LEFT');  end;    end.