DirectInput, detecting alt-tab

Started by
5 comments, last by brekehan 21 years, 7 months ago
Hiyas, I am trying to toggle fullscreen in my game when the user hits alt-tab, however I cannot find an ascii code for alt anywhere. how would I do this? I can also use DirectInput key codes but don''t have a chart for them. it would be diks[index]. Thanx, Christopher Here is my snippet: // Read the Keyboard pInput->ReadKeyboard(); // If escape is pressed exit the game if(pInput->ascKeys[27]) PostQuitMessage(0); // If alt-tab swtich toggle full screen if(pInput->ascKeys[??] && pInput->ascKeys[??]) pDisplay->ToggleFullscreen(); // Draw the scene pDisplay->DrawGLScene();
Advertisement

Not answering your question I''m afraid, but over-riding alt-tab is not a good thing to do.

Tradition for toggling full screen is alt-enter. Its in tonnes of demos if you look for some source. I think it might even be in the code that the DirectX 8.1 new project wizard makes, so take a look (in the MsgProc obviously).
Here is the function I made in VB that returns the keyname, it might help you understand what some of the DISTATE values are.

  Public Function GetKeyName(ByVal iNum As Integer) As String Dim aKeys(255) As String, A As Integer If iNum > 255 Then iNum = 0 For A = 0 To 255   aKeys(A) = "{~" & CStr(A) & "}" Next A    aKeys(&H1) = "ESCAPE"    aKeys(&H2) = "1"    aKeys(&H3) = "2"    aKeys(&H4) = "3"    aKeys(&H5) = "4"    aKeys(&H6) = "5"    aKeys(&H7) = "6"    aKeys(&H8) = "7"    aKeys(&H9) = "8"    aKeys(&HA) = "9"    aKeys(&HB) = "0"    aKeys(&HC) = "-"             ' - on main keyboard    aKeys(&HD) = "="    aKeys(&HE) = "{BACK}"                  ' backspace    aKeys(&HF) = "{TAB}"    aKeys(&H10) = "Q"    aKeys(&H11) = "W"    aKeys(&H12) = "E"    aKeys(&H13) = "R"    aKeys(&H14) = "T"    aKeys(&H15) = "Y"    aKeys(&H16) = "U"    aKeys(&H17) = "I"    aKeys(&H18) = "O"    aKeys(&H19) = "P"    aKeys(&H1A) = "["    aKeys(&H1B) = "]"    aKeys(&H1C) = "{ENTER}" ' Enter on main keyboard    aKeys(&H1D) = "{LCTRL}"    aKeys(&H1E) = "A"    aKeys(&H1F) = "S"    aKeys(&H20) = "D"    aKeys(&H21) = "F"    aKeys(&H22) = "G"    aKeys(&H23) = "H"    aKeys(&H24) = "J"    aKeys(&H25) = "K"    aKeys(&H26) = "L"    aKeys(&H27) = ";"    aKeys(&H28) = "'"    aKeys(&H29) = "`" ' accent grave    aKeys(&H2A) = "{LSHIFT}"    aKeys(&H2B) = "\"    aKeys(&H2C) = "Z"    aKeys(&H2D) = "X"    aKeys(&H2E) = "C"    aKeys(&H2F) = "V"    aKeys(&H30) = "B"    aKeys(&H31) = "N"    aKeys(&H32) = "M"    aKeys(&H33) = ","    aKeys(&H34) = "." ' . on main keyboard    aKeys(&H35) = "/" ' / on main keyboard    aKeys(&H36) = "{RSHIFT}"    aKeys(&H37) = "*" ' * on numeric keypad    aKeys(&H38) = "{LALT}" ' left Alt    aKeys(&H39) = " "    aKeys(&H3A) = "{CAPSLOCK}"    aKeys(&H3B) = "{F1}"    aKeys(&H3C) = "{F2}"    aKeys(&H3D) = "{F3}"    aKeys(&H3E) = "{F4}"    aKeys(&H3F) = "{F5}"    aKeys(&H40) = "{F6}"    aKeys(&H41) = "{F7}"    aKeys(&H42) = "{F8}"    aKeys(&H43) = "{F9}"    aKeys(&H44) = "{F10}"    aKeys(&H45) = "{NUMLOCK}"    aKeys(&H46) = "{SCROLL}" ' Scroll Lock    aKeys(&H47) = "7" ' num pads    aKeys(&H48) = "8"    aKeys(&H49) = "9"    aKeys(&H4A) = "-" ' - on numeric keypad    aKeys(&H4B) = "4"    aKeys(&H4C) = "5"    aKeys(&H4D) = "6"    aKeys(&H4E) = "+" ' + on numeric keypad    aKeys(&H4F) = "1"    aKeys(&H50) = "2"    aKeys(&H51) = "3"    aKeys(&H52) = "0"    aKeys(&H53) = "." ' . on numeric keypad    aKeys(&H56) = "{OEM_102}" ' German KBD?    aKeys(&H57) = "{F11}"    aKeys(&H58) = "{F12}"    aKeys(&H64) = "{F13}" ' NEC PC98?    aKeys(&H65) = "{F14}"    aKeys(&H66) = "{F15}"    aKeys(&H70) = "{KANA}" ' JAP KBD ?    aKeys(&H73) = "?" ' {ABNT_C1} ? on Portugese (Brazilian) keyboards    aKeys(&H79) = "{CONVERT}" '  on (Japanese keyboard)?    aKeys(&H7B) = "{NOCONVERT}|" '  on (Japanese keyboard)    aKeys(&H7D) = "{YEN}" ' Jap    aKeys(&H7E) = "." ' {ABNT_C2} on Numpad . on Portugese (Brazilian) keyboards    aKeys(&H8D) = "=" ' NUMPADEQUALS = on numeric keypad (NEC PC98)    aKeys(&H90) = "{PREVTRACK}" ' on Previous Track (CIRCUMFLEX on Japanese keyboard)    aKeys(&H91) = "@" ' AT (NEC PC98)    aKeys(&H92) = ":" ' COLON (NEC PC98) "    aKeys(&H93) = "_" ' (NEC PC98) "    aKeys(&H94) = "{KANJI}" ' KANJI on (Japanese keyboard)"    aKeys(&H95) = "{STOP}" ' (NEC PC98) "    aKeys(&H96) = "{AX}" ' (Japan AX) "    aKeys(&H97) = "{J3100}" ' Unlabeled    aKeys(&H99) = "{NEXTTRACK}" ' Next Track    aKeys(&H9C) = "{ENTER}" ' Enter on numeric keypad    aKeys(&H9D) = "{RCONTROL}"    aKeys(&HA0) = "{MUTE}" ' Mute    aKeys(&HA1) = "{CALCULATOR}" ' Calculator    aKeys(&HA2) = "{PLAYPAUSE}" ' Play / Pause    aKeys(&HA4) = "{MEDIASTOP}" ' Media Stop    aKeys(&HAE) = "{VOLUMEDOWN}" ' Volume -    aKeys(&HB0) = "{VOLUMEUP}" ' Volume +    aKeys(&HB2) = "{WEBHOME}" ' Web home    aKeys(&HB3) = "," ' , on numeric keypad (NEC PC98)    aKeys(&HB5) = "/" ' / on numeric keypad    aKeys(&HB7) = "{PrintScreen}"    aKeys(&HB8) = "{RALT}" ' right Alt    aKeys(&HC5) = "{PAUSE}" ' Pause    aKeys(&HC7) = "{HOME}" ' Home on arrow keypad    aKeys(&HC8) = "{UP}" ' UpArrow on arrow keypad    aKeys(&HC9) = "{PageUp}" ' PgUp on arrow keypad    aKeys(&HCB) = "{LEFT}" ' LeftArrow on arrow keypad    aKeys(&HCD) = "{RIGHT}" ' RightArrow on arrow keypad    aKeys(&HCF) = "{END}" ' End on arrow keypad    aKeys(&HD0) = "{DOWN}" ' DownArrow on arrow keypad    aKeys(&HD1) = "{PageDown}" ' PgDn on arrow keypad    aKeys(&HD2) = "{INSERT}" ' Insert on arrow keypad    aKeys(&HD3) = "{DELETE}" ' Delete on arrow keypad    aKeys(&HDB) = "{LWIN}" ' Left Windows key    aKeys(&HDC) = "{RWIN}" ' Right Windows key    aKeys(&HDD) = "{START}" ' AppMenu key    aKeys(&HDE) = "{POWER}" ' System Power    aKeys(&HDF) = "{SLEEP}" ' System Sleep    aKeys(&HE3) = "{WAKE}" ' System Wake    aKeys(&HE5) = "{WEBSEARCH}" ' Web Search    aKeys(&HE6) = "{WEBFAVORITES}" ' Web Favorites    aKeys(&HE7) = "{WEBREFRESH}" ' Web Refresh    aKeys(&HE8) = "{WEBSTOP}" ' Web Stop    aKeys(&HE9) = "{WEBFORWARD}" ' Web Forward    aKeys(&HEA) = "{WEBBACK}" ' Web Back    aKeys(&HEB) = "{MYCOMPUTER}" ' My Computer    aKeys(&HEC) = "{MAIL}" ' Mail    aKeys(&HED) = "{MEDIASELECT}" ' Media Select    GetKeyName = aKeys(iNum)End Function  


[Edit: Please use the source tag so people don't have to scroll too much. Thanks.]

[edited by - Oluseyi on September 9, 2002 9:15:43 PM]
To see the DirectInput key codes, just open up dinput.h or download the documentation. The codes are...

DIK_LALT
DIK_RALT

It doesn''t make sense to be using ASCII values with DirectInput.
Ok I can accept that. So say I want to use alt-enter, I still have no way of detecting the alt key

Another problem arose while I was playing with my code. It
seems that as soon as I implement DirectInput without having any kind of code doing anything to process keystrokes I cannot alt-tab in my program anyway,default behavior or not,it gives me an invalid page fault. I have no idea why since I have put error handling everywhere I can think of. Any ideas on that?
As an after thought perhaps alt is triggering a code > 255 and thus trying to change the value of my arrays out of bounds since they are declared as 255 elements? I shall play around some more.


  #ifndef INPUTSYSTEM_H#define INPUTSYSTEM_H//--------------------------------------------------------------------// InputSystem.h// // Declaration of the Input Processing Functions for the application //// Christopher Pisz// 04/08/02//--------------------------------------------------------------------#define DIRECTINPUT_VERSION      0x0800#include <windows.h>             // Windows API#include <dinput.h>              // DirectInput Library #define INPUTERROR_NODI          0x8100001    // No device error#define INPUTERROR_NOKEYBOARD    0x8100002    // No keyboard error#define KEYBOARD_BUFFERSIZE      10           // Keyboard buffer sizeclass InputSystem{	public:		BYTE                 diks[256];       // DInput Scan Codes		BYTE                 ascKeys[256];    // DInput Ascii Codes		InputSystem();		~InputSystem();		HRESULT InitDirectInput(HWND hWnd);   // Initialize Direct Input		HRESULT ReadKeyboard();               // Read the Keyboard	private:		LPDIRECTINPUT8       pDI;             // Pointer to DInput Interface		LPDIRECTINPUTDEVICE8 pKeyboard;       // Pointer to DInput Keyboard Device		HKL                  layout;          // Keyboard Layout};#endif INPUTSYSTEM_H//--------------------------------------------------------------------// InputSystem.cpp// // Implementation of the Input Processing Functions for the application //// Christopher Pisz// 04/08/02//--------------------------------------------------------------------#include "InputSystem.h"//--------------------------------------------------------------------// InputSystem//--------------------------------------------------------------------InputSystem::InputSystem(){	// Initialize variables	pDI        =NULL;           // Pointer to Device Interface	pKeyboard  =NULL;           // Pointer to keyboard device		// Set both key code arrays to all zeros (keys are up)	ZeroMemory(ascKeys,sizeof(ascKeys));	ZeroMemory(diks,sizeof(diks));}InputSystem::~InputSystem(){	// Release the Keyboard Device	if(pKeyboard)	{		pKeyboard->Unacquire();		pKeyboard->Release();	}	// Release The DirectInput Device Interface	if(pDI)		pDI->Release();}//---------------------------------------------------------------------// Initialize Direct Input//---------------------------------------------------------------------HRESULT InputSystem::InitDirectInput(HWND hWnd){	DIPROPDWORD dipw;	// Create a DirectInput Interface	if(!pDI)	{		if(FAILED(DirectInput8Create(GetModuleHandle(NULL),DIRECTINPUT_VERSION,			IID_IDirectInput8,(void**)&pDI,NULL)))			return INPUTERROR_NODI;	}	// Get the keyboard layout	layout=GetKeyboardLayout(0);	// Obtain an interface to the system keyboard device	if(FAILED(pDI->CreateDevice(GUID_SysKeyboard,&pKeyboard,NULL)))		return INPUTERROR_NOKEYBOARD;	// Create buffer to hold the keyboard data	ZeroMemory(&dipw,sizeof(DIPROPDWORD));	dipw.diph.dwSize           =sizeof(DIPROPDWORD);	dipw.diph.dwHeaderSize     =sizeof(DIPROPHEADER);	dipw.diph.dwObj            =0;	dipw.diph.dwHow            =DIPH_DEVICE;	dipw.dwData                =10;	// Set the size of the buffer	if(FAILED(pKeyboard->SetProperty(DIPROP_BUFFERSIZE,&dipw.diph)))		return INPUTERROR_NOKEYBOARD;	// Set the format of the keyboard	if(FAILED(pKeyboard->SetDataFormat(&c_dfDIKeyboard)))		return INPUTERROR_NOKEYBOARD;	// Set the cooperative level to exclusive access	if(FAILED(pKeyboard->SetCooperativeLevel(hWnd,DISCL_EXCLUSIVE | DISCL_FOREGROUND)))		return INPUTERROR_NOKEYBOARD;	// Aquire the keyboard device	pKeyboard->Acquire();	return S_OK;}//---------------------------------------------------------------------// Read the Keyboard//---------------------------------------------------------------------HRESULT InputSystem::ReadKeyboard(){	DWORD                   dwCurBuffer;	DIDEVICEOBJECTDATA      didKeyboardBuffer[KEYBOARD_BUFFERSIZE];	DWORD                   dwItems=KEYBOARD_BUFFERSIZE;	BYTE                    byteASCII;	// Don't try to read the keyboard if the device is invalid	if(!pKeyboard || !pDI)		return INPUTERROR_NOKEYBOARD;	// Read the buffered data	pKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),		didKeyboardBuffer,&dwItems,0);	// Process data if there is data to read	if(dwItems)	{		for(dwCurBuffer=0;dwCurBuffer<dwItems;dwCurBuffer++)		{			// Map scan-code to ASCII code			byteASCII=MapVirtualKeyEx(didKeyboardBuffer[dwCurBuffer].dwOfs,				1,layout);			// Set key to be down (pressed)			if(didKeyboardBuffer[dwCurBuffer].dwData & 0x80)			{				ascKeys[byteASCII]=1;				diks[didKeyboardBuffer[dwCurBuffer].dwOfs]=1;			}			// Set Key to be up			else			{				ascKeys[byteASCII]=0;				diks[didKeyboardBuffer[dwCurBuffer].dwOfs]=0;			}		}	}	return S_OK;}  


[Edit: GDNet tags use square brackets - [] - not angular ones. That's why you didn't get the desired effect.]

[edited by - Oluseyi on September 9, 2002 9:16:36 PM]
quote:Original post by brekehan
Hiyas,
I am trying to toggle fullscreen in my game when the user hits
alt-tab, however I cannot find an ascii code for alt anywhere.
how would I do this?

The alt+tab combination generates the Windows message WM_ACTIVATE. Simply handle that inside your WndProc.
Ok all is well. I appreicate all the good responses. I ended up having an error because of the size of the arrays. I increased them to 512 and it works fine now using the dik codes. I decided not to overide the behavior of alt-tab for the reasons stated by the posters and instead to use alt-enter. I already had code in my winproc for the WM_ACTIVATE that I didnt want to alter and its probably alot better off that way. I wanted to include the ascii codes because it makes it easier to write functions for capturing typed text, making textbox functionality and such but I''ll use the dik codes for most input. I am off to do my
"The Bug is Squashed Dance"
Thanx,
Christopher

This topic is closed to new replies.

Advertisement