Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help: Triggers, show/hide w/hotkeys, etc
#1
Hi, I'm at a stopping point with this.
Idea: Allow hotkeys (here Ctrl+Shift+K and Ctrl+Shift+L) to show and hide a window. Use triggers. Use a button to populate a TextBox with the window's own HWND.
Working so far: the hotkeys show and hide the window.
Problems and general not-working stuff:
  • The window doesn't show at all until I hit Ctrl+Shift+K. After that, show/hide works fine.
  • The TextBox, if clicked, shows the HWND (I didn't want that to happen until I click the button, and in the second code block that does work fine).
  • After clicking in the TextBox (but only after), the TextBox finally updates.
  • On exiting with the 'x' in the titlebar, the script stays running. I suspect that has to do with my feeble Trigger attempts, but I'm not sure, of course.
The problem code:
 
Code:
Copy      Help
//.
using System.Windows.Controls;
using System.Windows;
using Au.Triggers;
using System.Windows.Media;

script.setup(trayIcon: true, sleepExit: true);
//..

ActionTriggers Triggers = new();
var hk = Triggers.Hotkey;

var b = new wpfBuilder("Window Handle Test");

b.Brush(Brushes.Moccasin);
b.WinProperties(topmost: true);
b.R.Add(out TextBox t1, "Text").Height(150).Width(150).Focus();
b.R.Add(out Button btnGetHwnd, "Change TextBox Text to HWND");
btnGetHwnd.Click += (o, e) => Button_Click(o, e);
b.End();
IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(b.Window).EnsureHandle();

//.
b.Loaded += () => {
    //Triggers.Options.Thread(0, 500);
    var wih = new System.Windows.Interop.WindowInteropHelper(b.Window);
    var hWnd2 = wih.Handle;
    t1.Text = hWnd2.ToString();
    //Triggers.Of.AllWindows(); //let the following triggers work with all windows
    hk["Ctrl+Shift+K"] = o => {
        ShowWindow(hWnd2.ToInt32(), 9);
        print.it($"{o} was pressed.");
    };
    hk.Last.EnabledAlways = true;
    hk["Ctrl+Shift+L"] = o => {
        ShowWindow(hWnd2.ToInt32(), 6);
        print.it($"{o} was pressed.");
    };
    Triggers.Run();
};
//..
b.ShowDialog();
Triggers.Stop();

void Button_Click(object sender, RoutedEventArgs e)
{
    var wih = new System.Windows.Interop.WindowInteropHelper(b.Window);
    var hWnd = wih.Handle;
    t1.Text += Environment.NewLine +  hWnd.ToString();
}

// There is probably a better way ...
[DllImport("User32")]
static extern int ShowWindow(int hwnd, int nCmdShow);

//    SW_MINIMIZE = 6,
//    SW_RESTORE = 9,

The working code (no triggers):
 
Code:
Copy      Help
//.
using System.Windows.Controls;
using System.Windows;

script.setup(trayIcon: true, sleepExit: true);
//..

var b = new wpfBuilder("Window Handle test");
b.R.Add(out TextBox t1, "Text").Height(50).Width(150);
b.R.Add(out Button btnHwnd, "Get HWND");
btnHwnd.Click += (o, e) => Button_Click(o, e);
b.End();
b.ShowDialog();

void Button_Click(object sender, RoutedEventArgs e)
{
    var hWnd = new System.Windows.Interop.WindowInteropHelper(b.Window).EnsureHandle();
    t1.Text += Environment.NewLine +  hWnd.ToString();
}

Thanks for taking the time to look at this!
Best regards,
burque505
#2
Code:
Copy      Help
// script ""
//.
using System.Windows.Controls;
using System.Windows;
using Au.Triggers;
using System.Windows.Media;

script.setup(trayIcon: true, sleepExit: true);
//..

// build window


var b = new wpfBuilder("Window Handle Test");
var w = b.Window;

b.Brush(Brushes.Moccasin);
b.WinProperties(topmost: true);
b.R.Add(out TextBox t1, "Text").Size(150, 150).Focus();
b.R.AddButton("Change TextBox Text to HWND", _ => {
    t1.Text += Environment.NewLine + (nint)w.Hwnd();
});

b.End();

//.
b.Loaded += () => {
    var h = w.Hwnd();
    t1.Text = h.Handle.ToString();
};

//..

// set triggers


ActionTriggers Triggers = new();
Triggers.Options.ThreadThis();
var hk = Triggers.Hotkey;
hk["Ctrl+Shift+K"] = o => {
    var h = w.Hwnd();
    h.ShowNotMinimized();
    print.it($"{o} was pressed.");
};

hk.Last.EnabledAlways = true;
hk["Ctrl+Shift+L"] = o => {
    var h = w.Hwnd();
    h.ShowMinimized();
    print.it($"{o} was pressed.");
};


w.Closed += (_, _) => { Triggers.Stop(); };

//show window and run triggers. Two good ways.

#if !true
w.Show();
Triggers.RunThread();
#else
run.thread(() => { Triggers.Run(); });
b.ShowDialog();
#endif
#3
Thanks very much indeed, Gintaras. Your code not only works perfectly but answered a lot of questions for me.
Best regards,
burque505


Forum Jump:


Users browsing this thread: 1 Guest(s)