# Bug: spurious KEYDOWN events on gaining focus

**URL:** <https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700>\
**Category:** SDL Development\
**Created:** [July 12, 2001, 12:44am UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700 "2001-07-12T00:44:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maciej\_Kalisiak](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@Maciej\_Kalisiak](https://discourse.libsdl.org/u/Maciej_Kalisiak)\
**Post date:** [July 12, 2001, 12:44am UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/1 "2001-07-12T00:44:41Z")

</div>

I noticed that SDL (1.2) is generating KEYDOWN events when it shouldn’t.

I’m using “icewm” window manager under Linux 2.2.19, XFree86 4.0.3. In this  
window manager I have keybindings such that “Alt+Tab” cycles to next app,  
while “Alt+” goes to desktop . If I tab out to another app,  
and then back to an SDL one, SDL will erroneously generate a “Tab” KEYDOWN on  
reentry. Another example: let’s say my SDL app is on desktop 1, I do "Alt+4"  
to go to desktop 4, then do “Alt+1” to get back, SDL will generate a KEYDOWN  
for “1”.

I ran “xev” and went throught the same sequence, and no such extra KEYDOWN  
events were noted, so I don’t think it’s the window manager misguidedly  
passing them down somehow.–  
Maciej Kalisiak mac at [dgp.toronto.edu](http://dgp.toronto.edu)[www.dgp.toronto.edu/~mac](http://www.dgp.toronto.edu/~mac)

---

<div class="post-metadata">

**Author:** ![Mattias\_Engdegard](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Mattias\_Engdegard](https://discourse.libsdl.org/u/Mattias_Engdegard)\
**Post date:** [July 12, 2001, 9:10am UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/2 "2001-07-12T09:10:14Z")

</div>

> I’m using “icewm” window manager under Linux 2.2.19, XFree86 4.0.3. In this  
> window manager I have keybindings such that “Alt+Tab” cycles to next app,  
> while “Alt+” goes to desktop . If I tab out to another app,  
> and then back to an SDL one, SDL will erroneously generate a “Tab” KEYDOWN on  
> reentry.

SDL keeps an internal map of which keys are currently pressed (this is  
what you get with SDL\_GetKetState). When your window loses focus, all  
keys are considered released so synthetic KEYUP events are generated  
where necessary. When it regains focus, SDL checks what keys are still  
being held down, and generates synthetic KEYDOWN events for these,  
so that the application won’t see sudden KEYUP events for keys that it  
doesn’t think are being pressed

If this is undesirable we could change it so that keyup events for  
non-pressed keys are ignored, but I prefer not changing too much  
of the semantics in the 1.2 branch

---

<div class="post-metadata">

**Author:** ![Mikko\_Rantalainen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@Mikko\_Rantalainen](https://discourse.libsdl.org/u/Mikko_Rantalainen)\
**Post date:** [July 12, 2001, 11:37am UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/3 "2001-07-12T11:37:17Z")

</div>

Hi!

I grab frames from video grabber card and show then out through  
YUVOverlay. However, images are a little too dark [1] and afaik xvideo  
supports changing brightness (and hue and contrast) of overlays  
independent of gamma. Is there some undocumented (at least I couldn’t find  
any documented one) function to do this via SDL? If not any suggestions  
for a workaround? Should I guess video port [2] that’s in use and try to  
change it’s settings?

I’m using SDL 1.2.0 with Linux 2.4.5/XFree 4.1.0 w/ Matrox G400.

- Mikko

[1] Images are too dark when shown in screen. Gamma setting doesn’t affect  
overlays and I need to tweek INPUT (for example increase values to get  
dark shades instead of pure black) for overlay instead of OUTPUT that is  
visible. This practically decreases usable bit depth and decreases image  
quality.

[2] Yeah, I know that G400 only has one video port but in case that this  
software is used in another system I’d want to do The Right Thing™.

---

<div class="post-metadata">

**Author:** ![Maciej\_Kalisiak](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/ad7895/32.png) [@Maciej\_Kalisiak](https://discourse.libsdl.org/u/Maciej_Kalisiak)\
**Post date:** [July 12, 2001, 2:25pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/4 "2001-07-12T14:25:15Z")

</div>

> SDL keeps an internal map of which keys are currently pressed (this is  
> what you get with SDL\_GetKetState). When your window loses focus, all  
> keys are considered released so synthetic KEYUP events are generated  
> where necessary. When it regains focus, SDL checks what keys are still  
> being held down, and generates synthetic KEYDOWN events for these,  
> so that the application won’t see sudden KEYUP events for keys that it  
> doesn’t think are being pressed

Ahhhh. Hmm, well, it seems this way that if one causes an SDL application to  
regain focus through some key combination, the app will always generate these  
extra KEYDOWNs. Even on a Windows machine, if one Alt+Tabs to the  
application, it sounds like the extra “Tab” will be generated. I would think  
this would be a common occurrence then.

> If this is undesirable we could change it so that keyup events for  
> non-pressed keys are ignored,

That would be great.

> but I prefer not changing too much of the semantics in the 1.2 branch

In that case, could it perhaps be made somehow a runtime option? I’m thinking  
of something like “SDL\_Option(NO\_KEYUPS\_ON\_LOSE\_FOCUS)” or something of  
similar intent. That way legacy SDL apps go on the way they were, while newer  
apps have the choice of picking the functionality they want.On Thu, Jul 12, 2001 at 05:10:14AM -0400, Mattias EngdegUrd wrote:

–  
Maciej Kalisiak mac at dgp.toronto.edu www.dgp.toronto.edu/~mac

---

<div class="post-metadata">

**Author:** ![Eike\_Sauer](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/94ad74/32.png) [@Eike\_Sauer](https://discourse.libsdl.org/u/Eike_Sauer)\
**Post date:** [July 13, 2001, 3:13pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/5 "2001-07-13T15:13:00Z")

</div>

Hi!

> If not any suggestions for a workaround?

You could adjust the data itself and add a certain  
constant/percentage to every Y value (and cut it down  
to 255 if necessary).  
I’m not sure if that’s what you are looking for…

Ciao,  
Eike

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [July 13, 2001, 3:49pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/6 "2001-07-13T15:49:38Z")

</div>

> > I’m using “icewm” window manager under Linux 2.2.19, XFree86 4.0.3. In this  
> > window manager I have keybindings such that “Alt+Tab” cycles to next app,  
> > while “Alt+” goes to desktop . If I tab out to another app,  
> > and then back to an SDL one, SDL will erroneously generate a “Tab” KEYDOWN on  
> > reentry.

> SDL keeps an internal map of which keys are currently pressed (this is  
> what you get with SDL\_GetKetState). When your window loses focus, all  
> keys are considered released so synthetic KEYUP events are generated  
> where necessary. When it regains focus, SDL checks what keys are still  
> being held down, and generates synthetic KEYDOWN events for these,  
> so that the application won’t see sudden KEYUP events for keys that it  
> doesn’t think are being pressed

> If this is undesirable we could change it so that keyup events for  
> non-pressed keys are ignored, but I prefer not changing too much  
> of the semantics in the 1.2 branch

This behavior is actually breaking a bunch of applications (including  
some Loki games), so I would like to revert back to the original behavior  
of not generating synthetic keydowns after losing keyboard focus.

SDL should already ignore key up events for keys that it doesn’t think  
are down. The original reason for this code was to keep the modifier  
keys in sync, so that ctrl, numlock, etc. are in the correct state on  
startup and also when regaining input focus.

Maybe we can do something different? Maybe just set the modifier state  
when gaining input focus?

See ya,  
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

---

<div class="post-metadata">

**Author:** ![Mattias\_Engdegard](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Mattias\_Engdegard](https://discourse.libsdl.org/u/Mattias_Engdegard)\
**Post date:** [July 13, 2001, 4:52pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/7 "2001-07-13T16:52:58Z")

</div>

> This behavior is actually breaking a bunch of applications (including  
> some Loki games), so I would like to revert back to the original behavior  
> of not generating synthetic keydowns after losing keyboard focus.

yes I’m convinced

> Maybe we can do something different? Maybe just set the modifier state  
> when gaining input focus?

that’s probably the best thing to do. I’d also like to see if it’s  
possible to generate keyup/keydown events for locking keys each time  
they are toggled, instead of just handing out a keydown event when  
it’s turned on, and a keyup event when it’s turned off. It also implies  
that the locking keys should not have their modifier state in the  
keystate vector but work as any other key if possible

---

<div class="post-metadata">

**Author:** ![Mikko\_Rantalainen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@Mikko\_Rantalainen](https://discourse.libsdl.org/u/Mikko_Rantalainen)\
**Post date:** [July 13, 2001, 7:07pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/8 "2001-07-13T19:07:50Z")

</div>

This is exactly what I’m currently doing (I wasn’t exactly clear in in my  
original posting; I used word INPUT when I meant “Y values written to YUV  
overlay” and OUTPUT when I really meant “real intensity displayed on  
screen”.). There are some problems though…

1. I’m missing some precision because I cannot use full 8 bit range of Y  
values but instead like 30-255 [range of 0-30 would be all black anyway]  
(this is minor problem because I’m using analog source that has enough  
noise to cover some missed bits, but it can make difference when I add  
some noise reduction code)

2. Even if I send 255 to overlay it won’t be as bright as 255,255,255 to  
RGB surface. This is because overlay would require more brightness and  
this cannot be fixed with adjust to input values.

3. Because this could be done with hardware through xvideo it’s a real  
shame to need to fix it with CPU simply because SDL doesn’t have API for  
this. Because I’m grabbing 768x576 25fps and doing some image processing  
in real time I need every cycle I can get - ultimate target would be to  
generate 50fps through intelligent deinterlacing routine but at least  
currently it would take too much CPU power.

Also, middle range colors are too dark but I’d need gamma setting to fix  
this because brightness and contrast are linear controls and cannot move  
middle point of color ramp nearer to other end.

What I’m looking for is some way to really modify xvideo brightness and  
contrast. If SDL doesn’t support this then I any suggestions about how to  
do it without official support would be appreciated. I’m meaning  
workarounds in spirit of GTK hack via environment variable.

It’s Friday night and English isn’t my native language but I hope you  
understand what’s the problem…

- MikkoOn Fri, 13 Jul 2001, Eike R. Sauer wrote:

> > If not any suggestions for a workaround?
> 
> You could adjust the data itself and add a certain  
> constant/percentage to every Y value (and cut it down  
> to 255 if necessary).  
> I’m not sure if that’s what you are looking for…

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [July 13, 2001, 7:18pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/9 "2001-07-13T19:18:29Z")

</div>

> Hi!

> I grab frames from video grabber card and show then out through  
> YUVOverlay. However, images are a little too dark [1] and afaik xvideo  
> supports changing brightness (and hue and contrast) of overlays  
> independent of gamma. Is there some undocumented (at least I couldn’t find  
> any documented one) function to do this via SDL?

I don’t see an API to do this using XVideo. Am I missing something?

See ya,  
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

---

<div class="post-metadata">

**Author:** ![didier](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/41988e/32.png) [@didier](https://discourse.libsdl.org/u/didier)\
**Post date:** [July 14, 2001, 12:49am UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/10 "2001-07-14T00:49:35Z")

</div>

Hi  
Sam Lantinga wrote:

> > Hi!
> 
> > I grab frames from video grabber card and show then out through  
> > YUVOverlay. However, images are a little too dark [1] and afaik xvideo  
> > supports changing brightness (and hue and contrast) of overlays  
> > independent of gamma. Is there some undocumented (at least I couldn’t find  
> > any documented one) function to do this via SDL?
> 
> I don’t see an API to do this using XVideo. Am I missing something?

XvQueryPortAttributes, XvSetPortAttribute

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [July 14, 2001, 8:41pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/11 "2001-07-14T20:41:43Z")

</div>

> > I don’t see an API to do this using XVideo. Am I missing something?
> 
> XvQueryPortAttributes, XvSetPortAttribute

Ahh, I didn’t know you could use this to set that.  
Well, if you can write up an implementation that can set the brightness  
for the YUV overlay that works with Linux XVideo, Windows DirectX, and  
the SDL software code, then I’ll be happy to add it.

See ya!  
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [July 14, 2001, 8:43pm UTC](https://discourse.libsdl.org/t/bug-spurious-keydown-events-on-gaining-focus/4700/12 "2001-07-14T20:43:54Z")

</div>

> > This behavior is actually breaking a bunch of applications (including  
> > some Loki games), so I would like to revert back to the original behavior  
> > of not generating synthetic keydowns after losing keyboard focus.

> yes I’m convinced

> > Maybe we can do something different? Maybe just set the modifier state  
> > when gaining input focus?

Okay, this is now implemented in CVS. The correct keyboard and modifier  
state is set when the application starts up and when the keyboard focus  
is gained. Changing how the capslock and numlock keys function will wait  
until SDL 1.3

To sum up, you no longer get spurious key down events on X11.  
I’m accepting patches that have similar functionality for the other  
platforms, too. 🙂

See ya!  
-Sam Lantinga, Lead Programmer, Loki Software, Inc.
