# Keyevents

**URL:** <https://discourse.libsdl.org/t/keyevents/7778>\
**Category:** SDL Development\
**Created:** [November 21, 2002, 11:53am UTC](https://discourse.libsdl.org/t/keyevents/7778 "2002-11-21T11:53:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henry\_Gomersall](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/h/ccd318/32.png) [@Henry\_Gomersall](https://discourse.libsdl.org/u/Henry_Gomersall)\
**Post date:** [November 21, 2002, 11:53am UTC](https://discourse.libsdl.org/t/keyevents/7778/1 "2002-11-21T11:53:01Z")

</div>

Do I need a screen to be open to use the key events?

If I just want a console program to read keys as they are pressed, how  
do I do this in SDL? I have a program that reads the keys when I have a  
screen open, however, the code didn’t seem to work when I tried without  
the screen.

thanks

Henry Gomersall  
-------------- next part --------------  
A non-text attachment was scrubbed…  
Name: not available  
Type: application/pgp-signature  
Size: 189 bytes  
Desc: This is a digitally signed message part  
URL: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20021121/282e6a7f/attachment.pgp](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20021121/282e6a7f/attachment.pgp)

---

<div class="post-metadata">

**Author:** ![Darrell\_Walisser](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/darrell_walisser/32/588_2.png) [@Darrell\_Walisser](https://discourse.libsdl.org/u/Darrell_Walisser)\
**Post date:** [November 21, 2002, 12:12pm UTC](https://discourse.libsdl.org/t/keyevents/7778/2 "2002-11-21T12:12:02Z")

</div>

> Do I need a screen to be open to use the key events?

Yes.

> If I just want a console program to read keys as they are pressed, how  
> do I do this in SDL? I have a program that reads the keys when I have a  
> screen open, however, the code didn’t seem to work when I tried without  
> the screen.

If you just want to read keyboard input from the console, you don’t  
need SDL. Use the scanf/fscanf/getc functions instead:

while(1) {

```
char buffer[32];

printf ("Enter your name: ");
fflush (stdout);

scanf ("%s\n", buffer);

printf ("Your name is: %s\n", buffer);

```

}On Thursday, November 21, 2002, at 02:51 PM, Henry Gomersall wrote:

---

<div class="post-metadata">

**Author:** ![Samuel\_E\_Bray](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/8491ac/32.png) [@Samuel\_E\_Bray](https://discourse.libsdl.org/u/Samuel_E_Bray)\
**Post date:** [November 21, 2002, 11:46pm UTC](https://discourse.libsdl.org/t/keyevents/7778/3 "2002-11-21T23:46:03Z")

</div>

You don’t need a screen open, try something like…

SDL\_Event \*event

while(SDL\_Pollevent(event)  
{  
switch(event-\>type)  
{  
case SDL\_KEYDOWN:  
switch(event-\>key-\>keysym)  
{  
case SDLK\_a:  
printLetter(‘A’); /\* pretend function _/  
break;  
case SDLK\_b:  
printLetter(‘B’);  
break;  
case SDLK\_c:  
printLetter(‘C’);  
break;  
case SDLK\_q:  
case SDLK\_ESCAPE:  
SDL\_Quit();  
exit(0);  
break; /_ just for looks _/  
case SDLK\_F1:  
doSomething(); /_ not real \*/  
break;  
default:  
break;  
}  
default:  
break;  
}  
}

I wrote a program (and accidently compiled right over  
the source key…damn my “itchy tab-key finger”) that  
just tested for keyboard input and printed a response  
with sdl.

more info can be found via the man pages and the  
online documentation

> Do I need a screen to be open to use the key events?
> 
> If I just want a console program to read keys as  
> they are pressed, how  
> do I do this in SDL? I have a program that reads the  
> keys when I have a  
> screen open, however, the code didn’t seem to work  
> when I tried withoutOn Thursday 21 November 2002 11:51 am, Henry Gomersall wrote:  
> the screen.
> 
> thanks
> 
> Henry Gomersall

–  
Hello, I’m your mind.  
Sorry I haven’t been around much,  
but I’m easily distracted by shiny objects.  
-The Tick’s Mind

* * *

Do you Yahoo!?  
Yahoo! Mail Plus ? Powerful. Affordable. Sign up now.

> **[Yahoo Mail](https://overview.mail.yahoo.com/)**
>
> Now sleeker, smarter, and simpler - the redesigned Yahoo Mail desktop experience helps you stay organized and makes everyday life easier with features like a Priority Inbox, AI-generated one-line summaries, Quick Action shortcuts and more.
