Elektrozoider
Member
- Joined
- Jun 4, 2015
- Messages
- 114
- Reaction score
- 10
Great code, now can you make it a toggle instead of while pressed?
Great idea yours!, yes now that you commented that I also think is better to use a kind of pause-toogle functionality instead of a "pause the bot until key is released", yes, this way we will gain full usability of keyboard.
So I did the necessary modifications and here below is the new code that you've demanded

I totally replaced the functionality to follow your suggestion, I also added the current key name to the plugin description, and a simple log message when the bot is paused or resumed, and now when the bot is paused the main thread is stopped during 500 ms to avoid any next pause/resume while the key is hold.
(sorry for my bad English, i hope all was good understood)
LetMeDrive v1.1:
Code:
#region " Namespace Imports "
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Input;
using Key = System.Windows.Input.Key;
using Zeta.Bot;
using Zeta.Bot.Logic;
using Zeta.Bot.Profile;
using Zeta.Common;
using Zeta.Common.Plugins;
using Zeta.TreeSharp;
using log4net;
#endregion
namespace LetMeDrive {
#region " P/Invoking "
internal sealed class SafeNativeMethods {
internal const int KEY_PRESSED = 0x8000;
[SuppressUnmanagedCodeSecurity()]
[DllImport("user32.dll", SetLastError = false)]
internal static extern IntPtr GetForegroundWindow();
[SuppressUnmanagedCodeSecurity()]
[DllImport("user32.dll", SetLastError = false)]
internal static extern int GetWindowThreadProcessId(IntPtr hwnd, out int pid);
[SuppressUnmanagedCodeSecurity()]
[DllImport("user32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
internal static extern short GetKeyState(int virtualKey);
//[SuppressUnmanagedCodeSecurity()]
//[DllImport("user32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
//internal static extern short GetAsyncKeyState(int virtualKey);
}
#endregion
public sealed class Logic : IPlugin {
#region " User Fields "
private readonly Key userKey = Key.LeftCtrl;
#endregion
#region " Private Fields "
private readonly ILog logger = Logger.GetLoggerInstanceForType();
private bool isBotPaused = false;
#endregion
#region " Properties "
public string Name {
get { return string.Format("LetMeDrive {0}", Version.ToString()); }
}
public Version Version {
get { return new Version(1, 1); }
}
public string Author {
get { return "Elektro"; }
}
public string Description {
get { return string.Format("Lets you pause the bot on demand using '{0}' key.", this.userKey.ToString()); }
}
public Window DisplayWindow {
get { return null; }
}
#endregion
#region " Event Invocators "
public void OnInitialize() {}
public void OnEnabled() {
this.logger.Info("[LetMeDrive] Plugin enabled.");
}
public void OnDisabled() {
this.isBotPaused = false;
this.logger.Info("[LetMeDrive] Plugin disabled.");
}
public void OnShutdown() {
this.isBotPaused = false;
}
public void OnPulse() {
if ((!BotMain.IsPaused && IsPauseRequested()) || BotMain.IsPaused && !IsPauseRequested()) {
BotMain.PauseWhile(IsPauseRequested);
}
}
#endregion
#region " Public Methods "
public bool Equals(IPlugin other) {
return (other.Name == Name) && (other.Version == Version);
}
#endregion
#region " Private Methods "
private bool IsPauseRequested() {
int virtualKey = System.Windows.Input.KeyInterop.VirtualKeyFromKey(this.userKey);
bool keyIsPressed = Convert.ToBoolean(SafeNativeMethods.GetKeyState(virtualKey) & SafeNativeMethods.KEY_PRESSED);
if (keyIsPressed) {
Process[] d3procs = Process.GetProcessesByName(processName: "Diablo III");
if (d3procs.Any()) {
int[] d3pids = (from p in d3procs select p.Id).ToArray();
IntPtr activeWindow = SafeNativeMethods.GetForegroundWindow();
int activePid = 0;
SafeNativeMethods.GetWindowThreadProcessId(activeWindow, out activePid);
if (activePid != 0) {
Process activeProc = Process.GetProcessById(activePid);
if (d3pids.Contains(activeProc.Id)) {
this.isBotPaused = !this.isBotPaused;
this.logger.Info(string.Format("[LetMeDrive] Key '{0}' pressed, new bot status: {1}",
this.userKey.ToString(), (this.isBotPaused ? "Paused" : "Resumed")));
Thread.Sleep(500);
}
}
}
}
return this.isBotPaused;
}
#endregion
}
}
Last edited: