Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Keyboard Management In Pascal


  • Please log in to reply
1 reply to this topic

#1 Pjp


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Learning:Pascal

Posted 31 May 2012 - 06:11 AM

I'm a beginner in Pascal and I'm working on a small WIngraph game. At some point on the game, the character (which is a block) has to lay down (the block gets half of its original height). I want this to happen while holding the arrow-down key but the way I implemented it is not actually working. Another problem I have is I don't know how to read keys simultaneously (that would be needed when, for example, running to the right and jumping).

That's how I tried to write it:

case key of
arrow down code#:repeat
block gets half of its height
main loop of the program (drawing shapes etc)
until keypressed = false; <-- that's the problem. I expected the code to run fine while the key was pressed and as soon as its released, the block should get its normal height and then the program would run based on its main loop, but outside of this procedure.

Everything is working, except that key-holding thing.

Thank you!
  • 0

#2 Luthfi


    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 22 June 2012 - 10:52 PM

Hi Pjp, welcome to CodeCall.

Information about the compiler will be helpful here. Basically you need a timer to simulate repeating keypress. Activate the timer when key is down and deactivate it when the key is up (released). In Delphi it would be something like this:

Event handler for the form's OnKeyDown event.
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  Timer1.Enabled := True;

Event handler for the form's OnKeyUp event.

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  Timer1.Enabled := False;

And event handler for Timer1's OnTimer:
procedure TForm1.Timer1Timer(Sender: TObject);
  vKey: Word;
  AShift: TShiftState;
  vKey := ... // Get what key is currently pressed
  vShift := ... // Get which extended keys (Shift, Ctrl, Alt, etc) that also pressed at the time

  case ... 
    // here do whatever you need to do if the detected keyboard button

  • 0

Also tagged with one or more of these keywords: keyboard

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download