From d1d90b2ce8156d93e45b524284a364700678e89b Mon Sep 17 00:00:00 2001
From: ForgottenIce <38312721+ForgottenIce@users.noreply.github.com>
Date: Tue, 12 Mar 2019 23:59:10 +0100
Subject: [PATCH] Hotkeys Update
Hotkeys now only trigger if the game is focused and the game isn't paused.
---
Rayman2LevelSwitcher/MainWindow.xaml | 2 +-
Rayman2LevelSwitcher/MainWindow.xaml.cs | 58 +++++++++++++++++++++++--
Rayman2LevelSwitcher/Memory.cs | 6 +++
3 files changed, 61 insertions(+), 5 deletions(-)
diff --git a/Rayman2LevelSwitcher/MainWindow.xaml b/Rayman2LevelSwitcher/MainWindow.xaml
index c75b65c..c9f1761 100644
--- a/Rayman2LevelSwitcher/MainWindow.xaml
+++ b/Rayman2LevelSwitcher/MainWindow.xaml
@@ -126,7 +126,7 @@
-
+
diff --git a/Rayman2LevelSwitcher/MainWindow.xaml.cs b/Rayman2LevelSwitcher/MainWindow.xaml.cs
index 35bb18c..5d039bf 100644
--- a/Rayman2LevelSwitcher/MainWindow.xaml.cs
+++ b/Rayman2LevelSwitcher/MainWindow.xaml.cs
@@ -49,6 +49,8 @@ public partial class MainWindow : Window {
Thread bgThread;
+ bool hotkeysEnabled = false;
+
public MainWindow()
{
InitializeComponent();
@@ -70,15 +72,63 @@ public void BookmarkUpdater()
//Update bookmarks every second
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(() => UpdateBookmarks() ));
+ //Dispatcher.Invoke(DispatcherPriority.Normal,
+ // new Action(() => txtblock_currentbookmarklevel.Text = Rayman2Handle()));
+ //Dispatcher.Invoke(DispatcherPriority.Normal,
+ // new Action(() => txtblock_currentbookmarklevel.Text += " " + Memory.GetForegroundWindow().ToString()));
Thread.Sleep(1000);
}
}
+ private bool Rayman2IsFocused()
+ {
+ const int nChars = 256;
+ StringBuilder Buff = new StringBuilder(nChars);
+ IntPtr handle = Memory.GetForegroundWindow();
+
+ if (Memory.GetWindowText(handle, Buff, nChars) > 0)
+ {
+ if (Buff.ToString() == "Rayman II")
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private bool Rayman2IsPaused()
+ {
+ int processHandle = GetRayman2ProcessHandle(false);
+ if (processHandle < 0) { return false; }
+
+ int bytesReadOrWritten = 0;
+
+ byte[] pausePointerBuffer = new byte[4];
+
+ Memory.ReadProcessMemory((int)processHandle, off_voidpointer, pausePointerBuffer, pausePointerBuffer.Length, ref bytesReadOrWritten);
+
+ if (BitConverter.ToInt32(pausePointerBuffer, 0) == 1)
+ {
+ return true;
+ }
+ return false;
+ }
+
+ private void chk_hotkeys_Checked(object sender, RoutedEventArgs e)
+ {
+ hotkeysEnabled = true;
+ }
+
+ private void chk_hotkeys_Unchecked(object sender, RoutedEventArgs e)
+ {
+ hotkeysEnabled = false;
+ }
+
private void OnKeyPressed(object sender, GlobalKeyboardHookEventArgs e)
{
//Debug.WriteLine(e.KeyboardData.VirtualCode);
- if (!chk_hotkeys.IsChecked.Value) {
+ if (!hotkeysEnabled || Rayman2IsPaused() || !Rayman2IsFocused() && !Application.Current.MainWindow.IsActive) {
e.Handled = false;
return;
}
@@ -533,7 +583,7 @@ private void LoadBookmark()
private void RenameBookmark()
{
renameBookmarkName = "";
- bool hotkeysIsChecked = chk_hotkeys.IsChecked ?? true;
+ bool hotkeysIsChecked = hotkeysEnabled;
int processHandle = GetRayman2ProcessHandle(false);
if (processHandle < 0 || !File.Exists(bookmarkFile) || listbox_bookmarklist.SelectedItem == null || listbox_bookmarklist.SelectedItems.Count > 1)
{
@@ -553,9 +603,9 @@ private void RenameBookmark()
rename.Left = Application.Current.MainWindow.Left + (Application.Current.MainWindow.Width / 2) - (rename.Width / 2);
rename.Top = Application.Current.MainWindow.Top + (Application.Current.MainWindow.Height / 2) - (rename.Height / 2);
rename.txtbox_name.Text = listbox_bookmarklist.SelectedItem.ToString();
- chk_hotkeys.IsChecked = false;
+ hotkeysEnabled = false;
rename.ShowDialog();
- chk_hotkeys.IsChecked = hotkeysIsChecked;
+ hotkeysEnabled = hotkeysIsChecked;
if (!String.IsNullOrEmpty(renameBookmarkName) && listbox_bookmarklist.SelectedItem.ToString() != renameBookmarkName)
{
var xml = XDocument.Load(bookmarkFile);
diff --git a/Rayman2LevelSwitcher/Memory.cs b/Rayman2LevelSwitcher/Memory.cs
index c2a3350..9ba50b6 100644
--- a/Rayman2LevelSwitcher/Memory.cs
+++ b/Rayman2LevelSwitcher/Memory.cs
@@ -25,6 +25,12 @@ public static extern bool ReadProcessMemory(int hProcess,
public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress,
byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesWritten);
+ [DllImport("user32.dll")]
+ public static extern IntPtr GetForegroundWindow();
+
+ [DllImport("user32.dll")]
+ public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
+
public static int GetPointerPath(int processHandle, int baseAddress, params int[] offsets)
{
int currentAddress = baseAddress;