Disable Alt-Tab from minimizing a full-screen game
I’m a C# developer and also a computer gamer.
Playing Warhammer (now in beta) it is common to have several keys assigned to alt-numbers. Right hand on the mouse left hand doing the alt-number combinations. It is easy to hit alt-tab when going for alt-1 epically in combat situations and absolutely the very worst time to have the game minimize.
I would like to disable alt-tab from taking me out of Warhammer.
Preferably without losing the ability of using either key, as both have uses. It is only the combination minimizing the game that needs to be avoided.
How can it be done?
My main machine is running Vista 64, so the solution needs to work on that platform.- Edited byShawn Sabbini Saturday, August 23, 2008 8:05 PMtypo
Answers
- Run this Windows Forms app before you start gaming:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
private const int MYKEYID = 0; // In case you want to register more than one...
public Form1() {
InitializeComponent();
RegisterHotKey(this.Handle, MYKEYID, MOD_ALT, Keys.Tab);
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
UnregisterHotKey(this.Handle, MYKEYID);
}
protected override void WndProc(ref Message m) {
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == MYKEYID) {
Console.Beep();
}
base.WndProc(ref m);
}
// P/Invoke declarations
private const int WM_HOTKEY = 0x312;
private const int MOD_ALT = 1;
private const int MOD_CONTROL = 2;
private const int MOD_SHIFT = 4;
[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hWnd, int id, int modifier, Keys vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}
Hans Passant.- Marked As Answer byFigo FeiMSFT, ModeratorTuesday, August 26, 2008 3:42 AM
- Edited bynobugzMVP, ModeratorSunday, August 24, 2008 3:48 PM....
All Replies
- Run this Windows Forms app before you start gaming:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
private const int MYKEYID = 0; // In case you want to register more than one...
public Form1() {
InitializeComponent();
RegisterHotKey(this.Handle, MYKEYID, MOD_ALT, Keys.Tab);
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
UnregisterHotKey(this.Handle, MYKEYID);
}
protected override void WndProc(ref Message m) {
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == MYKEYID) {
Console.Beep();
}
base.WndProc(ref m);
}
// P/Invoke declarations
private const int WM_HOTKEY = 0x312;
private const int MOD_ALT = 1;
private const int MOD_CONTROL = 2;
private const int MOD_SHIFT = 4;
[DllImport("user32.dll")]
private static extern int RegisterHotKey(IntPtr hWnd, int id, int modifier, Keys vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}
Hans Passant.- Marked As Answer byFigo FeiMSFT, ModeratorTuesday, August 26, 2008 3:42 AM
- Edited bynobugzMVP, ModeratorSunday, August 24, 2008 3:48 PM....
Tried it with EVE, but its not preventing alt-tabbing out of the game.
- Odd, worked great when I tried it. Who's Eve?
Hans Passant. I compile the program for release. Run the exe outside Visual Studio. Then when I alt tab from any application, windows still takes me to the alt-tab window and moves focus to the next application.
But I found the problem. Your code must run as Administrator. If not, there is no error or message, it just doesn’t prevent alt-tab.
I can run it as Administrator, this is not a problem.
Thank you very much for your help.
P.S. EVE an MMO. REF: http://www.eve-online.com/I have a problem near enough to the in the OP. I have a web based app that is used in a Kiosk at the company gym. We have been having problems with people hitting alt-F4, windows key, Ctrl-Alt-Delete and the like.
We would like to lock this down, but have a way for a member of the local admin group to be able to get access to the system.
I am wondering if this app could do what we need and what I will need to add to get this to work.
The Vision;
An app that runs that will intercept the above key combos.
When Crtl-Alt-Delete is pressed, a dialog box would pop-up to allow people to use their NT User Name and password.
If they are a member of the local admin group, then disable the blocks.
If they are not, log the event and close the dialog.
We can’t interfere with the IE7 web app that the Gym users use.
- Edited byKmardis Wednesday, November 04, 2009 4:12 PMspelling


