I think you are looking at it from the wrong perspective. You could do what you are doing by starting a timer, which runs a loop, when a key is pressed, then stop the loop when the key is released, but that is alot of trouble.

I would just set a certain amount of "motion ( dx or dy )" to each key press, and set up a "repeat interval for the keys"). So when you hold the key down, you will get a succesion of events, which stop when the key is released.

Here is a super simple example just to give you the idea. Instead of printing the repeating key code, just assign your motion.

#!/usr/bin/perl use SDL::App; use SDL::Event; use SDL; ### Create A new Application window my $app = new SDL::App -width => 400, -height => 400; ### A simple event handler hash my $events = { SDL_KEYDOWN() => sub { my ($e) = @_; exit(0) if ( $e->key_sym() == SDLK_ESCAPE ); print $e->key_sym(),"\n"; }, SDL_QUIT() => sub { exit(); }, }; my $eventkr = new SDL::Event; $eventkr->set_key_repeat( 10, 10); ### Loop & process the events $app->loop($events); #$app->loop(); #no events, must kill to exit

I'm not really a human, but I play one on earth. flash japh

In reply to Re: SDL Events hold key down by zentara
in thread SDL Events hold key down by Treehunter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.