first commit
This commit is contained in:
40
Assets/02_Scripts/Desktop/GlobalExitHotkey.cs
Normal file
40
Assets/02_Scripts/Desktop/GlobalExitHotkey.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 전역 종료 핫키. 기본 Ctrl+Alt+Q.
|
||||
///
|
||||
/// 작업표시줄에서 숨겨진 데다 WS_EX_NOACTIVATE 까지 붙으면 창이 포커스를
|
||||
/// 받지 못해 Unity 의 키 입력이 들어오지 않는다. GetAsyncKeyState 는 포커스와
|
||||
/// 무관하게 전역 키 상태를 읽으므로, 메시지 루프나 wndproc 후킹 없이
|
||||
/// 확실한 탈출구가 된다.
|
||||
/// </summary>
|
||||
public class GlobalExitHotkey : MonoBehaviour
|
||||
{
|
||||
// 아래 필드들은 Windows 빌드에서만 쓰이므로 에디터 컴파일 시 CS0414 가 뜬다
|
||||
#pragma warning disable 0414
|
||||
|
||||
[Tooltip("A-Z 한 글자. Ctrl+Alt 와 조합된다")]
|
||||
[SerializeField] char exitKey = 'Q';
|
||||
|
||||
[SerializeField] bool requireCtrl = true;
|
||||
[SerializeField] bool requireAlt = true;
|
||||
|
||||
#if !UNITY_EDITOR && UNITY_STANDALONE_WIN
|
||||
bool quitting;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (quitting) return;
|
||||
|
||||
if (requireCtrl && !Win32.IsKeyDown(Win32.VK_CONTROL)) return;
|
||||
if (requireAlt && !Win32.IsKeyDown(Win32.VK_MENU)) return;
|
||||
|
||||
int vk = char.ToUpperInvariant(exitKey);
|
||||
if (!Win32.IsKeyDown(vk)) return;
|
||||
|
||||
quitting = true;
|
||||
Debug.Log("[GlobalExitHotkey] 종료 핫키 감지. 종료합니다.");
|
||||
Application.Quit();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user