ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Help with CopperCube
forum topic indicator Detecting when the game is tabbed out
person icon
nazrin6
Registered User
Quote
2026-05-01 17:08:41

Is there a way to detect when the player tabs out of the game? I'm having a problem where sounds will keep playing but the timer stops when the game is tabbed out, leading to everything not being synced up correctly. emoji icon_cry

person icon
gazzauk
Registered User
Quote
2026-05-03 21:43:08

ccbRegisterKeyUpEvent for ALT TAB keys and then call ccbStopSound.

person icon
Guest2
Guest
Quote
2026-05-08 00:39:51

Based on gazzauk's suggestion, here is a non-optimal script-based workaround for the issue described by nazrin6. The script is small & very basic. Further adjustments may be necessary.
If someone has a better workaround, please post it.

A real solution for the problem might involve a modification of how the engine handles sound-playback when the game window loses/regains focus. Like "pausing" & "unpausing" the audio thread or internally calling ccbPauseAllSounds().

Problem description:
When the CopperCube game window loses focus, the game appears to enter a "sleep" mode, halting the game-loop & scripts but active sounds continue to play.

What this script-setup does:
When the ALT key or the WINDOWS key is pressed (which usually happens when the player wants to "leave" the game window), ALL sounds are paused.
ALL sounds are resumed when the keys are no longer held down and the game-loop runs normally - this usually happens when the player returns to the game window and the game wakes up from "sleep" mode.

IMPORTANT:
- Works for the Windows exe target. Tested in windowed & fullscreen mode.
- Pauses / unpauses ALL sounds - via ccbPauseAllSounds().

Known issues:
- CTRL+ALT+DEL may exit the window and reselect it in the background, circumventing this workaround.
- In fullscreen: Holding ALT down without pressing TAB will pause all sounds while the game keeps running. Uncomment the lines with "ccbSetGameTimerSpeed()" to pause/unpause physics & movement as a partial workaround for this problem.
- Any other cool methods that allow you to "leave" the game window are not captured by this script.
- If the user repeatedly tabs out & returns to the game, audio may still get out of sync as the small timing differences caused by the cooldown-counter would probably accumulate.

Notes:
- On my system, TAB (keyCode = 9) is never registered by the engine when ALT+TAB is pressed.
- In windowed mode: Pressing ALT triggers the game window to become inactive. This does not happen in fullscreen mode.
- Internally, the engine appears to use the GetActiveWindow()-function in "user32.dll" to check if the game window is active.

How to use this script:
- Select the root scene node
- Add a behavior "Before first drawing do something"
- Check: AlsoOnReload
- Action: Execute Javascript

Javascript:

ccbRegisterKeyDownEvent("keyPressedGeneric");
ccbRegisterOnFrameEvent(onFrameDrawing);

var global_sound_pause_state = 0;

function pauseSounds() {

ccbPauseAllSounds(true);
global_sound_pause_state = 30; //cooldown to prevent triggering unpauseSounds() while window focus changes, value found empirically by trial & error, 30 appears to work for a game running with 60 fps
//ccbSetGameTimerSpeed(0.0); //uncomment to pause physics & movement
}

function unpauseSounds() {

ccbPauseAllSounds(false);
global_sound_pause_state = 0;
//ccbSetGameTimerSpeed(1.0); //uncomment to resume physics & movement
}

function keyPressedGeneric(keyCode)
{

//print(keyCode);
if (keyCode == 91) { pauseSounds(); } //Win key
if (keyCode == 164) { pauseSounds(); } //Alt key

}


function onFrameDrawing()
{

if (global_sound_pause_state >= 1) {
if (global_sound_pause_state == 1) { unpauseSounds(); }
global_sound_pause_state = global_sound_pause_state - 1;
}

}



Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |