Recent

Author Topic: Can i press 2 key at the same time?  (Read 2866 times)

Emperor2503

  • Newbie
  • Posts: 6
Can i press 2 key at the same time?
« on: November 19, 2021, 09:13:11 am »
So there's a character when u press left keys, he going to left, when you press right keys, he going to right, when u press up keys, he going to jump.
so the problem is, when i press up keys and left keys(both together at the same time), only left keys work.
So how to make them work together when we press both key together at the same time?
(Sorry I'm new at programming)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Can i press 2 key at the same time?
« Reply #1 on: November 19, 2021, 09:17:57 am »
Depends entirely on your operating system and hardware.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Emperor2503

  • Newbie
  • Posts: 6
Re: Can i press 2 key at the same time?
« Reply #2 on: November 19, 2021, 09:24:14 am »
uhm I don't get what did you mean, sorry.
But can you take a look my program that I post? Untitled2.pas one

Emperor2503

  • Newbie
  • Posts: 6
Re: Can i press 2 key at the same time?
« Reply #3 on: November 19, 2021, 09:36:20 am »
At the program, can I press button #72 and button #77 at the same time?

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: Can i press 2 key at the same time?
« Reply #4 on: November 19, 2021, 09:53:45 am »
Я не помню насчёт Readkey. В простых графических программах, при опросе клавиатуры, делаю флаги указывающие произошедшее событие. Четыре события (up, left, right, down) и для них четыре флага, указывающие, что они были нажаты в данный момент времени.
Первое: вам надо убрать из "main" при опросе все "else" и опрашивать клавиши последовательно.
Второе: надо будет произвести выборку когда нажаты диаметрально противоположные клавиши и обнулить их.
Третье: вам нужна будет процедура, которая будет обрабатывать флаги с учётом произошедших событий.

При выполнении этих условий персонаж сможет двигаться по диагонали.

В вашем коде это надо дополнительно переделывать.

Тут есть одна проблема... Я в программах проверяю клавиши отжатия и отключаю флаги нажатий... как это будет происходить в данном случае?
Code: Pascal  [Select][+][-]
  1.   if keyDown = Left then
  2.     flagLeft := true
  3.   else
  4.     flagLeft := false;
может быть в подобном варианте?

google translate:
I don't remember about the Readkey. In simple graphical programs, when polling the keyboard, I make flags indicating the event that occurred. Four events (up, left, right, down) and for them four flags indicating that they were pressed at a given time.
First: you need to remove all "else" from "main" when polling and poll the keys sequentially.
Second: it will be necessary to make a selection when diametrically opposite keys are pressed and reset them.
Third: you will need a procedure that will process the flags taking into account the events that have occurred.

If these conditions are met, the character will be able to move diagonally.

In your code, this needs to be further redone.

There is one problem here... I check the pressing keys in the programs and disable the pressing flags... how will this happen in this case?
Code: Pascal  [Select][+][-]
  1.   if keyDown = Left then
  2.         flag Left := true
  3.   else
  4.     flagLeft := false;
can it be in a similar variant?
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

Emperor2503

  • Newbie
  • Posts: 6
Re: Can i press 2 key at the same time?
« Reply #5 on: November 19, 2021, 10:16:44 am »
I still don't get it %)
can you give me another example? or can you explain to me more detail?
I'm new at programming, I'm so sorry :(
Edit : I'm using dev-pascal
« Last Edit: November 19, 2021, 10:33:23 am by Emperor2503 »

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Can I press two keys at the same time?
« Reply #6 on: November 19, 2021, 10:39:22 am »
uhm I don't get what did you mean, sorry.
Just No. It won’t work reliably. For more on the underlying technical issue https://en.wikipedia.org/wiki/Rollover_(keyboard)
Yours Sincerely
Kai Burghardt

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can i press 2 key at the same time?
« Reply #7 on: November 19, 2021, 11:29:23 am »
Can be done easily in Lazarus.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Graphics, ExtCtrls, LCLType;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     shpSprite: TShape;
  16.     shpUp: TShape;
  17.     shpDown: TShape;
  18.     shpLeft: TShape;
  19.     shpRight: TShape;
  20.     Timer1: TTimer;
  21.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  24.     procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  25.     procedure Timer1Timer(Sender: TObject);
  26.   private
  27.     FKup, FKdown, FKleft, FKright: Boolean;
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  40.   );
  41. begin
  42.   case Key of
  43.     VK_UP:    FKup    := True;
  44.     VK_DOWN:  FKdown  := True;
  45.     VK_LEFT:  FKleft  := True;
  46.     VK_RIGHT: FKright := True;
  47.   end;
  48. end;
  49.  
  50. procedure TForm1.FormCreate(Sender: TObject);
  51. begin
  52.   FKup                  := False;
  53.   FKdown                := False;
  54.   FKleft                := False;
  55.   FKright               := False;
  56.   Timer1.Interval       := 20;
  57.   shpSprite.Shape       := stCircle;
  58.   shpSprite.Brush.Color := clGreen;
  59. end;
  60.  
  61. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  62. begin
  63.   Timer1.Enabled := False;
  64. end;
  65.  
  66. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  67. begin
  68.   case Key of
  69.     VK_UP:    FKup    := False;
  70.     VK_DOWN:  FKdown  := False;
  71.     VK_LEFT:  FKleft  := False;
  72.     VK_RIGHT: FKright := False;
  73.   end;
  74. end;
  75.  
  76. procedure TForm1.Timer1Timer(Sender: TObject);
  77. begin
  78.  
  79.   if FKup then
  80.     shpUp.Brush.Color := clRed
  81.   else
  82.     shpUp.Brush.Color := clWhite;
  83.   if FKdown then
  84.     shpDown.Brush.Color := clRed
  85.   else
  86.     shpDown.Brush.Color := clWhite;
  87.   if FKleft then
  88.     shpLeft.Brush.Color := clRed
  89.   else
  90.     shpLeft.Brush.Color := clWhite;
  91.   if FKright then
  92.     shpRight.Brush.Color := clRed
  93.   else
  94.     shpRight.Brush.Color := clWhite;
  95.  
  96.   if FKup    then shpSprite.Top  := shpSprite.Top - 1;
  97.   if FKdown  then shpSprite.Top  := shpSprite.Top + 1;
  98.   if FKleft  then shpSprite.Left := shpSprite.Left - 1;
  99.   if FKright then shpSprite.Left := shpSprite.Left + 1;
  100.  
  101.   if shpSprite.Top < -shpSprite.Height then
  102.     shpSprite.Top := Height;
  103.   if shpSprite.Top > Height then
  104.     shpSprite.Top := -shpSprite.Height;
  105.   if shpSprite.Left < -shpSprite.Width then
  106.     shpSprite.Left := Width;
  107.   if shpSprite.Left > Width then
  108.     shpSprite.Left := -shpSprite.Width;
  109.  
  110. end;
  111.  
  112. end.

Emperor2503

  • Newbie
  • Posts: 6
Re: Can i press 2 key at the same time?
« Reply #8 on: November 19, 2021, 11:41:49 am »
So at dev-pascal it won't work :(
Okk so, i got another more question,
Did you guys know where i can learn more about lazarus(i just try to use it recently).
Something like unit, classes ,etc.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can i press 2 key at the same time?
« Reply #9 on: November 19, 2021, 11:58:46 am »

Emperor2503

  • Newbie
  • Posts: 6
Re: Can i press 2 key at the same time?
« Reply #10 on: November 19, 2021, 12:20:21 pm »
Ok thank you so much sirr :)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Can i press 2 key at the same time?
« Reply #11 on: November 20, 2021, 01:19:54 am »
Lovely example Handoko! You are an excellent ambassador for Free Pascal and Lazarus.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

 

TinyPortal © 2005-2018