3

I've been trying to send a focused window to the second monitor using the keyboard combination keys Shift+Win+LEFT using sendkeys in c#, but for some reason the window isn't moving to the second monitor. It works when I physically press on these keys on the keyboard but not pragmatically. Note I already know how to bring my desired window to foreground and it's working fine now I wanna send that desired window to the second monitor using the keyboard combination using c# Shift+Win+LEFT I don't want to use the SetWindowPos(proceso.MainWindowHandle, 0, monitor.Left, monitor.Top, monitor.Width, monitor.Height, 0);

here are my attempts that I've tried Thank you in advance looking forward for your help btw I'm still searching and trying.

trial 1:

private void btnSendToSecondDisplay_Click(object sender, EventArgs e)
{
  bringToFront("Task Manager");//This lets the "task manager" window to be in the foreground and has the focus set to it
  SendKeys.Send(Keys.ShiftKey.ToString());
  SendKeys.Send(Keys.LWin.ToString());
  SendKeys.Send("{LEFT}");
}

trial 2:

private void btnSendToSecondDisplay_Click(object sender, EventArgs e)
{
  bringToFront("Task Manager");//This lets the "task manager" window to be in the foreground and has the focus set to it
  SendKeys.Send("+"+Keys.LWin.ToString()+"{LEFT}");
}

trial 3:

private void btnSendToSecondDisplay_Click(object sender, EventArgs e)
{
  bringToFront("Task Manager");//This lets the "task manager" window to be in the foreground and has the focus set to it
  SendKeys.Send("+"+Keys.LWin.ToString()+"{LEFT}");
}

trial 4

private void btnSendToSecondDisplay_Click(object sender, EventArgs e)
{
  bringToFront("Task Manager");//This lets the "task manager" window to be in the foreground and has the focus set to it
  SendKeys.Send("{SHIFTDOWN}");
  SendKeys.Send("{APPSKEY}");
  SendKeys.Send("{LEFT}");
}

trial 5

private void btnSendToSecondDisplay_Click(object sender, EventArgs e)
{
  bringToFront("Task Manager");//This lets the "task manager" window to be in the foreground and has the focus set to it
  SendKeys.Send("{SHIFTDOWN}{APPSKEY}{LEFT}");
}

trial 6

private void btnSendToSecondDisplay_Click(object sender, EventArgs e)
{
  bringToFront("Task Manager");//This lets the "task manager" window to be in the foreground and has the focus set to it
  SendKeys.Send("+");
  SendKeys.Send(Keys.LWin.ToString());
  SendKeys.Send("{LEFT}");
}
user9223088
  • 39
  • 1
  • 3
  • I don't know for sure, but I think the problem is that the window itself isn't what processes those keys, so you're sending the keys to the wrong place. For example, a program's window doesn't handle Alt-Tab either. You can verify this by adding a `WndProc()` override to your form (you appear to be using Winforms), and checking two things: whether a) your window even gets the window message when you press those keys together, and b) if you do get the message(s), whether you're able to disable the behavior in your own window by _not_ passing the message(s) to the base proc. – Peter Duniho Jan 16 '18 at 08:51
  • I doubt you'll see the message at all, but even if you do, I'd guess you can't disable it. Meaning the foreground window proc isn't what's handling the keys, and so sending the keys to that window isn't going to do anything. – Peter Duniho Jan 16 '18 at 08:51
  • I didn't find the win key in the [SendKeys documentation](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send?view=netframework-4.7.1#System_Windows_Forms_SendKeys_Send_System_String_)... – Zohar Peled Jan 16 '18 at 08:52
  • the keyboard shortcut works on the task manager when i physically press Shift+Win key and then on the left arrow from my keyboard it sends whatever window has focus on it wither it's the task manager window, google chrome or any other window. I'm trying to do this pragmatically with the 'Sendkeys.Send'. I'm currently reading another post concerning 'SHIFTDOWN' and 'SHIFTUP' but I can't find 'LWinDOWN' and 'LWINUP' the only available Keys for the windows start key is the 'LWin' and 'RWin' – user9223088 Jan 16 '18 at 08:59
  • _"it sends whatever window has focus on it"_ -- no, I don't believe that's correct. You've _assumed_ that's what happens, because that's the window that has focus. But the OS itself has complete control over the input and can intercept it before it gets to the window; indeed, that's the only way a feature like that could work even for windows that don't call the default base window proc. As far as key up/down goes, in the `SendInput()` paradigm, the `KEYEVENTF_KEYUP` flag controls that. If present, the sent message is for key-up, if not, the sent message is for key-down. – Peter Duniho Jan 16 '18 at 09:02

1 Answers1

0

From MSDN

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

So your code should be something like this:

SendKeys.Send("+(^{ESC}{LEFT})");
Vanna
  • 746
  • 1
  • 7
  • 16
  • Except that there is no such thing as `{APPSKEY}` for `SendKeys`. That's an AutoHotKey thing. And even there, that's the context-menu key, not the window key. The above answer isn't going to help at all. – Peter Duniho Jan 16 '18 at 08:57
  • I answered OP's question. APPSKEY doesn't exist indeed, however. – Vanna Jan 16 '18 at 08:59
  • _"I answered OP's question"_ -- you posted an answer. That doesn't mean it was _useful_. – Peter Duniho Jan 16 '18 at 09:03
  • bingo found it. credits to https://stackoverflow.com/questions/10366152/sending-windows-key-using-sendkeys It is atcually CTRL+ ESC – Vanna Jan 16 '18 at 09:04
  • I've tried the above answer but still no luck how do I replace 'APPSKEY' with the real windows start key because it seems that there is no such things as 'APPSKEY' – user9223088 Jan 16 '18 at 09:06
  • @user9223088 read the duplicate question – Vanna Jan 16 '18 at 09:07
  • @Vanna I'll try the CTRL+ESC again because I think I tried it before but i'll do so again right now. Is there a difference between 'SendKeys' and 'SendSystemKeys' – user9223088 Jan 16 '18 at 09:08
  • @user9223088 Just by the looks of it, it seems they take different key names. – Vanna Jan 16 '18 at 09:11