Changing screen refresh rate with a keyboard shortcut

I have a little movie and tv-show collection and use the popular open-source mediacenter software Kodi to organize it. But as some tv-shows/movies have other image frequencies/fps than others, it necessary to change the screen refresh rate accordingly. Kodi already delivers an integrated functionality to automatically change the refresh rate as required.

Personally, I like to have some control over this, so I decided to develope a small windows application in C++ (with some C in order to work with the Win32 API) to accomplish this. Doing so a is definitely not a big task.

Initial requirements

As we make use of the windows API, we need to include the windows header files.

Taking a quick look at the documentations of a few functions we’ll use, shows that we need to include the library user32 into the compiler arguments. When using MinGW, the call will look something like this.

As the examples will contain some C++11 syntax, I also added the  proper parameter to compile these sources.

Changing the refresh rate

In order to change the refresh rate, you’ll first have to retrieve the available displays. You can do this by using the EnumDisplayDevices function. The displayId represents the index of the display. These indexes start with 0.

Next thing we need are the current display settings as we do not want to change anything but the display frequency. To accomplish this, we need to use EnumDisplaySettings function. As we’d like to query a specific display, we provide the DeviceName of the retrieved display.

Now having the currently active settings, we can simply change the property dmDisplayFrequency and call ChangeDisplaySettings with the updated struct.

Registering the keyboard shortcut

To register a keyboard shortcut, we need the RegisterHotKey function.

After registering the shortcut, we need to process particular events. To do this, we now create an event loop within our application. The struct variable wParam will hold the shortcut id. In this example, we won’t use it to actually distinguish between the different shortcuts but to directly provide the desired display frequency.

As the MinGW version I was using was missing the MOD_NOREPEAT definition, it had to be added manually.

Launching application

Now, we’re still missing the main function to fire everything up.

Create an executable without console

Finally we have to build our little application. As we’d like to have the application silently sit in the background, we don’t want a console to be displayed. To achieve this, we have to pass the argument -mwindows to our compiler.

This argument also ensures that the required windows libraries will be linked.

Example Code

I have attached two example codes. One which simply summarizes the above setup.

The other one is the actual implementation I’m using for my mediacenter setup. It also features small on-screen notifications showing the refresh rate that has been switched to. Rather than switching the frequency through multiple shortcuts, it cycles through a configured list.

Refresh Rate Changer Screenshot
Screenshot showing how the notification looks like when changing the refresh rate.

References

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.