Recent

Author Topic: Where i can find all # keys?  (Read 11038 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Where i can find all # keys?
« on: June 17, 2016, 07:54:01 pm »
I'd like to find all # keys because i'd like to use multiple keys in one readkey for example #18 is Alt+E (I know only this). But if i want to use CTRL,too, i don't know how to do.. Where i can know it? where i can find all these keys? Thanks.
Code: Pascal  [Select][+][-]
  1. uses crt;
  2. var
  3. input:char;
  4. begin
  5. repeat
  6. input:=readkey;
  7. if input=#18 then write('You've pressed Alt+E');
  8. until input=#77;    (*Right arrow key*);
  9. end.
  10.  
Answer please
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

RAW

  • Hero Member
  • *****
  • Posts: 867
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Where i can find all # keys?
« Reply #2 on: June 17, 2016, 08:07:57 pm »
I mean keys like arrow keys, esc key, enter key ecc. try to add this code to pascal
Code: Pascal  [Select][+][-]
  1. uses crt;
  2. var
  3. input:char;
  4. begin
  5. clrscr;
  6. repeat
  7. input:=readkey;
  8. until input=#19;
  9. write('done');
  10. readln;
  11. end.
  12.  
When running the console app then press Alt key and meanwhile the R key.. I mean something like this
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

jwdietrich

  • Hero Member
  • *****
  • Posts: 1133
    • formatio reticularis
Re: Where i can find all # keys?
« Reply #3 on: June 17, 2016, 08:47:57 pm »
Virtual key codes are platform-dependent. You can find a list for Windows at http://delphi.about.com/od/objectpascalide/l/blvkc.htm (it is for Delphi but should also apply to Lazarus / Free Pascal).

The sitiuation is different, however, for the ReadKey function. It returns a result of char type, i.e. an ASCII or ANSI code of the key pressed. If an extended or function key has been pressed, it returns the ASCII value 0. In this case, the scan code is delivered by a second call to ReadKey. See http://www.freepascal.org/docs-html/rtl/crt/readkey.html for details.

Codes may be different for platforms other than Windows.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.10 | FPC 3.2.0 | PPC, Intel, ARM | macOS, Windows, Linux

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Where i can find all # keys?
« Reply #4 on: June 17, 2016, 09:12:12 pm »
Here's a simple Example...
Code: Pascal  [Select][+][-]
  1. Program Console;
  2.  Uses Crt;
  3.   VAR
  4.    Key  : Char;
  5. Begin
  6.  WriteLn('Enter A Key:');
  7.   Repeat
  8.    Key:= Readkey;
  9.     Case Key
  10.     Of
  11.      #0    : Begin
  12.               Key:= ReadKey;
  13.                Case Key
  14.                Of
  15.                 #75 : WriteLn('Left');
  16.                 #77 : WriteLn('Right');
  17.                End;
  18.              End;
  19.      
  20.      #13   : WriteLn('Enter');
  21.      #8    : WriteLn('Backspace');
  22.      'r'   : WriteLn('R');
  23.     End;
  24.      
  25.   Until Key = #27 // ESC
  26. End.    
  27.  

If you want to use CTRL or ALT... I think you need Shift: TShiftstate and then ssALT, ssCTRL and so on ... I normally don't use Console programs...

EDIT:
Take a look at this: http://www.freepascal.org/docs-html/rtl/keyboard/getkeyevent.html
« Last Edit: June 17, 2016, 09:36:14 pm by SoE »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8266
  • Programming + Glam Metal + Tae Kwon Do = Me

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Where i can find all # keys?
« Reply #6 on: June 17, 2016, 10:18:23 pm »
For me it's not working... ??? But with Char-Input it's working fine...

Code: Pascal  [Select][+][-]
  1. Program MyKeyEvent;
  2.  Uses Keyboard;
  3.  
  4.  Var
  5.   Key: TKeyEvent;
  6. Begin
  7.  InitKeyboard;
  8.  WriteLn('Enter A Key:');
  9.   Repeat
  10.    Key:= GetKeyEvent;
  11.    Key:= TranslateKeyEvent(Key);
  12.    
  13.     Case GetKeyEventChar(Key)
  14.     Of
  15.      'r' : Writeln('r');
  16.      'R' : Writeln('R');
  17.      #8  : Writeln('Backspace');
  18.      #13 : Writeln('Enter');
  19.     End;
  20.    
  21.     Case Key  
  22.     Of
  23.      13: Writeln('ALT+R'); // ?????
  24.      12: Writeln('ALT+E'); // ?????
  25.     End;
  26.  
  27.   Until (GetKeyEventChar(Key)= #27);
  28.  DoneKeyBoard;
  29. End.      
  30.  
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Where i can find all # keys?
« Reply #7 on: June 18, 2016, 11:38:59 am »
I got it now.... :)

You can use this code to get any keycode:

Code: Pascal  [Select][+][-]
  1. Program CheckKeyCode;
  2.  Uses Keyboard;
  3.   Var
  4.    K: TKeyEvent;
  5. Begin
  6.  InitKeyboard;
  7.   WriteLn('Press a Key...');
  8.    Repeat
  9.     K:= GetKeyEvent;
  10.     K:= TranslateKeyEvent(K);
  11.      
  12.      WriteLn();
  13.      Write('Pressed Key: "');
  14.      Write(FunctionKeyName(TKeyRecord(K).KeyCode));
  15.      WriteLn('"');
  16.      Write('KeyCode: ');
  17.      WriteLn(TKeyRecord(K).KeyCode);
  18.    
  19.    Until GetKeyEventChar(K) = #27; // ESC
  20.  DoneKeyboard;
  21. End.  
  22.  

And then you can use this code to do something when a special key or key combination is pressed:

Code: Pascal  [Select][+][-]
  1. Program MyKeyEvent;
  2.  Uses Keyboard;
  3.   Var
  4.    Key: TKeyEvent;
  5. Begin
  6.  InitKeyboard;
  7.   WriteLn('Enter any key...');
  8.   WriteLn;
  9.    
  10.    Repeat
  11.     Key:= GetKeyEvent;
  12.     Key:= TranslateKeyEvent(Key);
  13.          
  14.      Case TKeyRecord(Key).KeyCode  
  15.      Of
  16.       4864  : WriteLn('ALT+R');
  17.       65314 : WriteLn('PageUp');
  18.       65313 : WriteLn('Up');
  19.       65281 : WriteLn('F1');
  20.      End;
  21.  
  22.    Until (GetKeyEventChar(Key)= #27); // ESC
  23.  DoneKeyBoard;
  24. End.
  25.  

This is working very fine on Windows 7... don't know what OS you are using... but check for yourself.
Good Luck...
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: Where i can find all # keys?
« Reply #8 on: June 18, 2016, 12:13:16 pm »
And of course you can use both (Char and ScanCode) at the same time... what is much easier for normal keys...

Code: Pascal  [Select][+][-]
  1. Program MyKeyEvent;
  2.  Uses Keyboard;
  3.   Var
  4.    Key: TKeyEvent;
  5. Begin
  6.  InitKeyboard;
  7.   WriteLn('Enter any key...');
  8.   WriteLn;
  9.    
  10.    Repeat
  11.     Key:= GetKeyEvent;
  12.     Key:= TranslateKeyEvent(Key);
  13.          
  14.      Case TKeyRecord(Key).KeyCode  
  15.      Of
  16.       4864  : WriteLn('ALT+R');
  17.       65314 : WriteLn('PageUp');
  18.       65313 : WriteLn('Up');
  19.       65281 : WriteLn('F1');
  20.      End;
  21.  
  22.      Case GetKeyEventChar(Key)
  23.      Of
  24.       'm'   : WriteLn('m');
  25.       'M'   : WriteLn('M');
  26.       #8    : WriteLn('Backspace');
  27.       #13   : WriteLn('Enter');
  28.       #39   : WriteLn(#39); // '
  29.      End;
  30.  
  31.    Until (GetKeyEventChar(Key)= #27); // ESC
  32.  DoneKeyBoard;
  33. End.        
  34.  
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

 

TinyPortal © 2005-2018