mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 17:43:05 +08:00
Merge pull request #9439 from Game4all/gameplay-disable-winkey
This commit is contained in:
commit
dfcd26be2d
@ -16,6 +16,7 @@ using osu.Framework.Logging;
|
|||||||
using osu.Framework.Screens;
|
using osu.Framework.Screens;
|
||||||
using osu.Game.Screens.Menu;
|
using osu.Game.Screens.Menu;
|
||||||
using osu.Game.Updater;
|
using osu.Game.Updater;
|
||||||
|
using osu.Desktop.Windows;
|
||||||
|
|
||||||
namespace osu.Desktop
|
namespace osu.Desktop
|
||||||
{
|
{
|
||||||
@ -98,6 +99,9 @@ namespace osu.Desktop
|
|||||||
LoadComponentAsync(versionManager = new VersionManager { Depth = int.MinValue }, Add);
|
LoadComponentAsync(versionManager = new VersionManager { Depth = int.MinValue }, Add);
|
||||||
|
|
||||||
LoadComponentAsync(new DiscordRichPresence(), Add);
|
LoadComponentAsync(new DiscordRichPresence(), Add);
|
||||||
|
|
||||||
|
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
|
||||||
|
LoadComponentAsync(new GameplayWinKeyBlocker(), Add);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ScreenChanged(IScreen lastScreen, IScreen newScreen)
|
protected override void ScreenChanged(IScreen lastScreen, IScreen newScreen)
|
||||||
|
41
osu.Desktop/Windows/GameplayWinKeyBlocker.cs
Normal file
41
osu.Desktop/Windows/GameplayWinKeyBlocker.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using osu.Framework.Allocation;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Platform;
|
||||||
|
using osu.Game.Configuration;
|
||||||
|
|
||||||
|
namespace osu.Desktop.Windows
|
||||||
|
{
|
||||||
|
public class GameplayWinKeyBlocker : Component
|
||||||
|
{
|
||||||
|
private Bindable<bool> allowScreenSuspension;
|
||||||
|
private Bindable<bool> disableWinKey;
|
||||||
|
|
||||||
|
private GameHost host;
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load(GameHost host, OsuConfigManager config)
|
||||||
|
{
|
||||||
|
this.host = host;
|
||||||
|
|
||||||
|
allowScreenSuspension = host.AllowScreenSuspension.GetBoundCopy();
|
||||||
|
allowScreenSuspension.BindValueChanged(_ => updateBlocking());
|
||||||
|
|
||||||
|
disableWinKey = config.GetBindable<bool>(OsuSetting.GameplayDisableWinKey);
|
||||||
|
disableWinKey.BindValueChanged(_ => updateBlocking(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateBlocking()
|
||||||
|
{
|
||||||
|
bool shouldDisable = disableWinKey.Value && !allowScreenSuspension.Value;
|
||||||
|
|
||||||
|
if (shouldDisable)
|
||||||
|
host.InputThread.Scheduler.Add(WindowsKey.Disable);
|
||||||
|
else
|
||||||
|
host.InputThread.Scheduler.Add(WindowsKey.Enable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
80
osu.Desktop/Windows/WindowsKey.cs
Normal file
80
osu.Desktop/Windows/WindowsKey.cs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace osu.Desktop.Windows
|
||||||
|
{
|
||||||
|
internal class WindowsKey
|
||||||
|
{
|
||||||
|
private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KdDllHookStruct lParam);
|
||||||
|
|
||||||
|
private static bool isBlocked;
|
||||||
|
|
||||||
|
private const int wh_keyboard_ll = 13;
|
||||||
|
private const int wm_keydown = 256;
|
||||||
|
private const int wm_syskeyup = 261;
|
||||||
|
|
||||||
|
//Resharper disable once NotAccessedField.Local
|
||||||
|
private static LowLevelKeyboardProcDelegate keyboardHookDelegate; // keeping a reference alive for the GC
|
||||||
|
private static IntPtr keyHook;
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Explicit)]
|
||||||
|
private readonly struct KdDllHookStruct
|
||||||
|
{
|
||||||
|
[FieldOffset(0)]
|
||||||
|
public readonly int VkCode;
|
||||||
|
|
||||||
|
[FieldOffset(8)]
|
||||||
|
public readonly int Flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int lowLevelKeyboardProc(int nCode, int wParam, ref KdDllHookStruct lParam)
|
||||||
|
{
|
||||||
|
if (wParam >= wm_keydown && wParam <= wm_syskeyup)
|
||||||
|
{
|
||||||
|
switch (lParam.VkCode)
|
||||||
|
{
|
||||||
|
case 0x5B: // left windows key
|
||||||
|
case 0x5C: // right windows key
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return callNextHookEx(0, nCode, wParam, ref lParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void Disable()
|
||||||
|
{
|
||||||
|
if (keyHook != IntPtr.Zero || isBlocked)
|
||||||
|
return;
|
||||||
|
|
||||||
|
keyHook = setWindowsHookEx(wh_keyboard_ll, (keyboardHookDelegate = lowLevelKeyboardProc), Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]), 0);
|
||||||
|
|
||||||
|
isBlocked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void Enable()
|
||||||
|
{
|
||||||
|
if (keyHook == IntPtr.Zero || !isBlocked)
|
||||||
|
return;
|
||||||
|
|
||||||
|
keyHook = unhookWindowsHookEx(keyHook);
|
||||||
|
keyboardHookDelegate = null;
|
||||||
|
|
||||||
|
keyHook = IntPtr.Zero;
|
||||||
|
|
||||||
|
isBlocked = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport(@"user32.dll", EntryPoint = @"SetWindowsHookExA")]
|
||||||
|
private static extern IntPtr setWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);
|
||||||
|
|
||||||
|
[DllImport(@"user32.dll", EntryPoint = @"UnhookWindowsHookEx")]
|
||||||
|
private static extern IntPtr unhookWindowsHookEx(IntPtr hHook);
|
||||||
|
|
||||||
|
[DllImport(@"user32.dll", EntryPoint = @"CallNextHookEx")]
|
||||||
|
private static extern int callNextHookEx(int hHook, int nCode, int wParam, ref KdDllHookStruct lParam);
|
||||||
|
}
|
||||||
|
}
|
@ -99,6 +99,7 @@ namespace osu.Game.Configuration
|
|||||||
Set(OsuSetting.ScoreDisplayMode, ScoringMode.Standardised);
|
Set(OsuSetting.ScoreDisplayMode, ScoringMode.Standardised);
|
||||||
|
|
||||||
Set(OsuSetting.IncreaseFirstObjectVisibility, true);
|
Set(OsuSetting.IncreaseFirstObjectVisibility, true);
|
||||||
|
Set(OsuSetting.GameplayDisableWinKey, true);
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
Set(OsuSetting.ReleaseStream, ReleaseStream.Lazer);
|
Set(OsuSetting.ReleaseStream, ReleaseStream.Lazer);
|
||||||
@ -229,6 +230,7 @@ namespace osu.Game.Configuration
|
|||||||
IntroSequence,
|
IntroSequence,
|
||||||
UIHoldActivationDelay,
|
UIHoldActivationDelay,
|
||||||
HitLighting,
|
HitLighting,
|
||||||
MenuBackgroundSource
|
MenuBackgroundSource,
|
||||||
|
GameplayDisableWinKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using osu.Framework;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
@ -78,6 +79,15 @@ namespace osu.Game.Overlays.Settings.Sections.Gameplay
|
|||||||
Bindable = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode)
|
Bindable = config.GetBindable<ScoringMode>(OsuSetting.ScoreDisplayMode)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows)
|
||||||
|
{
|
||||||
|
Add(new SettingsCheckbox
|
||||||
|
{
|
||||||
|
LabelText = "Disable Windows key during gameplay",
|
||||||
|
Bindable = config.GetBindable<bool>(OsuSetting.GameplayDisableWinKey)
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user