Enable/Disable Exclusive Keyboard Mode with InitKeyboard()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Enable/Disable Exclusive Keyboard Mode with InitKeyboard()

Post by nco2k »

hi fred,

setting the keyboard to exclusive mode, while using InitKeyboard() can be a nice thing, but also a huge barrier. especially with OpenWindowedScreen() instead of OpenScreen().

almost the whole system keys wont work and this can be a real pain in some situations. even if you can rebuild (almost) every system key function, there are still some missing parts, which simply cant be rebuild.

it would be nice to have a function to switch to exclusive mode and switch back to normal mode anytime. something similar to KeyboardMode() would be great. maybe a KeyboardExclusive(). :?:

like this code by stefan from german forum:

Code: Select all

#DISCL_BACKGROUND=8 
#DISCL_NONEXCLUSIVE=2 
#DISCL_EXCLUSIVE=1 
#DISCL_FOREGROUND=4 

Procedure GetDInputDevice();Returns the DirectInputDevice of the Keyboard. 
  Global *_DirectInput7A.IDirectInput7A 
  Global *_DirectInputDevice7A.IDirectInputDevice7A 
  !extrn _PB_DirectInput 
  !extrn _PB_Keyboard_InitState 
  !CMP dword[_PB_Keyboard_InitState],0 
  !JNZ InitStateOk 
  ProcedureReturn 0 
  !InitStateOk: 
  !MOV Eax,[_PB_DirectInput] 
  !MOV [p__DirectInput7A],Eax 
  If *_DirectInput7A\CreateDevice(?KeyboardGUID,@*DID2.IDirectInputDevice7A,0):ProcedureReturn 0:EndIf 
  Table=PeekL(*DID2)+OffsetOf(IDirectInputDevice7A\GetDeviceState()) 
  ReadProcessMemory_(GetCurrentProcess_(),Table,@GetDevState,4,@dummy) 
  Fake=?Fake 
  WriteProcessMemory_(GetCurrentProcess_(),Table,@Fake,4,@dummy) 
  ExamineKeyboard() 
  WriteProcessMemory_(GetCurrentProcess_(),Table,@GetDevState,4,@dummy) 
  *DID2\Release() 
  ProcedureReturn *_DirectInputDevice7A 
  InitKeyboard() 
  Fake: 
  !MOV Eax,[Esp+4] 
  !MOV [p__DirectInputDevice7A],Eax 
  !XOR Eax,Eax 
  !RET 12 
  DataSection 
  KeyboardGUID: 
  Data.l 1864182625,298833312,1162135487,21587 
  EndDataSection 
EndProcedure 

Procedure SwapToNonExclusiveKeyboard() 
  *Dev.IDirectInputDevice7A=GetDInputDevice() 
  *Dev\UnAcquire() 
  *Dev\SetCooperativeLevel(ScreenID(),#DISCL_NONEXCLUSIVE|#DISCL_FOREGROUND) 
  *Dev\Acquire() 
EndProcedure 








  
InitSprite() 
InitKeyboard() 


If OpenWindow(0, 0, 0, 800, 600, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "") = 0 : End : EndIf 
If OpenWindowedScreen(WindowID(), 0, 20, 800, 600, 1, 0, 0) = 0 : End : EndIf 

SwapToNonExclusiveKeyboard(); Muss nach OpenWindowedScreen() ausgeführt werden. 


If CreateMenu(0,WindowID()) 
  MenuTitle("&x") 
  MenuItem(0, "&y") 
  MenuItem(1, "&z") 
EndIf 

Repeat  
  Select WaitWindowEvent() 
    Case #PB_Event_Menu 
      Select EventMenuID() 
        Case 0 
          ;... 
          ;.. 
          ;. 
      EndSelect 
  EndSelect 
  
  ExamineKeyboard() ;ohne gehts 
Until KeyboardPushed(#PB_Key_Escape) 
but the problem here is, after taskswitching the exclusive keyboard mode is set again, so you have to check every time if the application was inactive and become active again to set non exclusive keyboard mode again. which would be a little bit ugly imho.

so an optional parameter for InitKeyboard(Exclusive #True/#False) would be also nice. the best way would be, to have them both, i guess:

Code: Select all

InitKeyboard(Exclusive) ;#True/#False
KeyboardExclusive(Exclusive) ;#True/#False
oh and btw, thanks for KeyboardMode() this is a great thing, for german keyboards. :D

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This is a good point. We will change the keyboard mode to non-exclusive for windowedscreen as it should behave like any other application (i don't think a SetExclusiveMode() is needed then, correct me if i miss something).
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

no its okay. non exclusive mode for windowed screen and exclusive mode for fullscreen, is a good compromise, at least for me.

but i dont know, if there are coders here, which would like to turn off exclusive mode even for fullscreen.

because of, i am using OpenWindowedScreen() even for fullscreen, i am happy anyway. thanks fred! :lol:

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
S.M.
Enthusiast
Enthusiast
Posts: 118
Joined: Sat Apr 24, 2004 1:11 pm
Contact:

Post by S.M. »

Thanks Fred ! :D
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Now, fullscreen and windowed screen use non-exclusive mode. A new mode have been added to KeyboardMode(), which allow to enable/disable the system keys (like 'Windows' keys). It's disable by default, to respect the old behaviour. It should be flexible enough now :wink:
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

@fred
great! any possibility to get the new (beta) lib? or do we have to wait until monday? :D

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

unfortunately, the library format has changed a bit so i can't put an updated lib somewhere. You will have to wait a bit :)
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post by nco2k »

hm yeah, i already thought something like that. just cant await the beta testing for v4. :wink:

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Fred wrote:unfortunately, the library format has changed a bit so i can't put an updated lib somewhere. You will have to wait a bit :)
Good to hear that there's a new lib format :)
Post Reply