I'm writing a very cheap and lame SDL game in perl mainly just out of being bored during certain periods of the day. I stubled across a problem and that is I can't seem to make it where the charater moves while the key is held down and stops moving when the key is released. I have tried various things so the code I'm attaching to this might have extra stuff that isn't exactly required.
use strict; use SDL; use SDL::App; use SDL::Rect; use SDL::Surface; use SDL::Color; use SDL::Event; my $hold = 0; my $key; my $right = 0; my $left = 0; my $up = 0; my $down = 0; my $x = 10; my $y; my $key2; my $gangsta = SDL::Surface->new( -name => 'gangsta.gif', ); my $ghetto = SDL::Surface->new( -name => 'ghetto.jpg', ); my $app = SDL::App->new( -height => $ghetto->height(), -width => $ghetto->width(), -depth => 16, -title => "China Man Shooter 0.2 (Gang Fights)", ); my $bg_rect = SDL::Rect->new( -height => $ghetto->height(), -width => $ghetto->width(), -x => 0, -y => 0, ); my $bg = SDL::Rect->new( -height => $app->height(), -width => $app->width(), -x => 0, -y => 0, ); my $gangsta_rect = SDL::Rect->new( -height => $gangsta->height(), -width => $gangsta->width(), -x => 0, -y => 0, ); my $gang = SDL::Rect->new( -height => $gangsta_rect->height(), -width => $gangsta_rect->width(), -x => 10, -y => 100, ); refresh(); my $event = new SDL::Event(); my %events = ( SDL_QUIT() => sub { exit(0); }, SDL_KEYDOWN() => sub { $event = shift; $key = $event->key_name(); current(); }, SDL_KEYUP() => sub { $event = shift; $key2 = $event->key_name(); }, ); $app->loop( \%events ); sub current { while ($hold == 0) { my $current_x = $gang->x(); my $current_y = $gang->y(); if ($key eq 'right') { for my $m ($current_x..($current_x + 5)) { $gang->x( $m ); refresh(); } } if ($key eq 'left') { for my $m ($current_x..($current_x - 5)) { $gang->x( $m ); refresh(); #I have no idea what im doing here %events->SDL_KEYUP(); if ($key eq $key2) { $hold = 1; } else { $hold = 0; } } } } } sub refresh { $ghetto->blit($bg_rect, $app, $bg); $gangsta->blit($gangsta_rect, $app, $gang); $app->update($bg, $gang); }
Basically I figured that In current I would run a while statment and some how check SDL_KEYUP to see when it would be equal to the previously pressed key but that hasn't worked. Any ideas?

Thanks for the Help its all works great now


In reply to 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.