Alt + Enter

Advanced game related topics
Shopro
Enthusiast
Enthusiast
Posts: 148
Joined: Tue May 13, 2003 8:05 am

Alt + Enter

Post by Shopro »

Hello,

Is there a easy way to switch from windowed screen to full screen mode with a "Alt + Enter"? This is my code, which doesn't work. pls do not run it, for it switches to and forth from windowed and fullscreen at great speed, and doesn't let you quit.

Code: Select all

ExamineKeyboard()

  Input_Alt_Enter = ((KeyboardPushed(#PB_Key_LeftAlt) Or 0) Or (KeyboardPushed(#PB_Key_RightAlt) Or 0)) And (KeyboardPushed(#PB_Key_Return) Or 0)


  If Input_Alt_Enter = 1
    If FullScreen = 1
      FullScreen = 0
      CloseScreen()
      OpenWindow(0, 0, 0, #Screen_Width, #Screen_Height, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered, #General_Title$)
      OpenWindowedScreen(WindowID(), 0, 0, #Screen_Width, #Screen_Height, 1, 0, 0)
      Load_Sprites()
      Input_Alt_Enter = 0
    ElseIf FullScreen = 0
      FullScreen= 1
      CloseWindow(0)
      CloseScreen()
      OpenScreen(#Screen_Width, #Screen_Height, 16, #General_Title$)
      Load_Sprites()
      Input_Alt_Enter = 0
    EndIf
  EndIf
Any advice will be appreciated :)

-Shopro
Ralf
Enthusiast
Enthusiast
Posts: 203
Joined: Fri May 30, 2003 1:29 pm
Location: Germany

Re: Alt + Enter

Post by Ralf »

purebasic dont suppot DOS coding where it was possible to switch by alt+return... you can swap screens by pressing alt+tab (standard on win)
Proteus
Enthusiast
Enthusiast
Posts: 113
Joined: Wed Sep 17, 2003 8:04 pm
Location: The Netherlands

Post by Proteus »

Shopro, did you put examinekeyboard() inside the loop?

Ralf, Alt+Tab switches programs...
P4 2.4GHz, 256 MB, WinXP Pro, onboard video&audio.
The Programmer's Drinking Song:
"99 little bugs in the code,
99 little bugs.
Fix one bug, recompile
100 little bugs in the code."
Shopro
Enthusiast
Enthusiast
Posts: 148
Joined: Tue May 13, 2003 8:05 am

Post by Shopro »

thanks for the replies,

Ralf:
I'm manualy trying to do that. Pls look at this code then:

Code: Select all

OpenWindow(0, 0, 0, 320, 240, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered, "Alt + Enter Input Test")
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(), 0, 0, 320, 240, 1, 0, 0)

Repeat
  
  ExamineKeyboard()
  Scan_WindowEvent = WindowEvent()

  Input_Alt_Enter = ((KeyboardPushed(#PB_Key_LeftAlt) Or 0) Or (KeyboardPushed(#PB_Key_RightAlt) Or 0)) And (KeyboardPushed(#PB_Key_Return) Or 0)
  
  StartDrawing(ScreenOutput())
  DrawText(Str(Input_Alt_Enter)) 
  StopDrawing()

  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or Scan_WindowEvent = #PB_Event_CloseWindow
This code returns 1 if Alt and Enter are pressed at the same time. It's the bit with the mode changing I am having trouble with.

Proteus:
yup, sure have:)
Shopro
Enthusiast
Enthusiast
Posts: 148
Joined: Tue May 13, 2003 8:05 am

Post by Shopro »

Did it:)

This code switches modes from Windowed to Fullscreen and vice-versa.
I had problems doing this because switching modes somehow seemed to lock the keyboard buffer, resulting in an endless loop of mode switching.
I'm not sure if this is a PB bug, or a Windows feature.

Code: Select all

; Sample Code to Switch Screen Modes
; 2003/10/11
; Kenji Gunn

; Constants
#True = 1
#False = 0

#Screen_Width.w = 320
#Screen_Height.w = 240
#Screen_Depth.b = 16

; Variables
Scan_Input_AltEnter.b = 0
Scan_Event_WindowEvent.w = 0

Screen_Fullscreen.b = 0

; Open Window
OpenWindow(0, 0, 0, #Screen_Width, #Screen_Height, #PB_Window_SystemMenu, "Hit Alt + Enter to Switch Screen Modes")

; Initialize DirectX
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(), 0, 0, 320, 240, 1, 0, 0)

Repeat
  
  ; Scan Key Presses and Window Events
  ExamineKeyboard()
  Scan_Input_AltEnter = ((KeyboardPushed(#PB_Key_LeftAlt) Or 0) Or (KeyboardPushed(#PB_Key_RightAlt) Or 0)) And (KeyboardPushed(#PB_Key_Return) Or 0)
  Scan_Event_WindowEvent = WindowEvent()
  
  ; Switch Screen Modes
  If Scan_Input_AltEnter = #True
    If Screen_Fullscreen = #True
       Screen_Fullscreen = #False
       CloseScreen() 
       OpenWindow(0, 0, 0, #Screen_Width, #Screen_Height, #PB_Window_SystemMenu, "Hit Alt + Enter to Switch Screen Modes") 
       OpenWindowedScreen(WindowID(), 0, 0, #Screen_Width, #Screen_Height, 0, 0, 0) 
     ElseIf Screen_Fullscreen = #False 
      Screen_Fullscreen = #True
      CloseWindow(0) 
      CloseScreen() 
      OpenScreen(#Screen_Width, #Screen_Height, #Screen_Depth, "Hit Alt + Enter to Switch Screen Modes") 
    EndIf
    Repeat
      ExamineKeyboard()
      Scan_Input_AltEnter = ((KeyboardPushed(#PB_Key_LeftAlt) Or 0) Or (KeyboardPushed(#PB_Key_RightAlt) Or 0)) And (KeyboardPushed(#PB_Key_Return) Or 0)
    Until Scan_Input_AltEnter = #True
  EndIf

  ; Flip Buffers
  FlipBuffers()
  
Until KeyboardPushed(#PB_Key_Escape) Or Scan_Event_WindowEvent = #PB_Event_CloseWindow

End
I solved the problem with this bit:

Code: Select all

Repeat
	ExamineKeyboard()
	Scan_Input_AltEnter = ((KeyboardPushed(#PB_Key_LeftAlt) Or 0) Or (KeyboardPushed(#PB_Key_RightAlt) Or 0)) And (KeyboardPushed(#PB_Key_Return) Or 0)
Until Scan_Input_AltEnter = #True
Wait until the keyboard buffer is unlocked:)

Thanks for all the advice

-Shopro
Post Reply