-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPostMessageHelper.cs
More file actions
49 lines (39 loc) · 1.95 KB
/
PostMessageHelper.cs
File metadata and controls
49 lines (39 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace KeepRDPAlive
{
class PostMessageHelper
{
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private const int WM_CHAR = 0x0102;
private const int WM_KEYUP = 0x0101;
private const int WM_KEYDOWN = 0x0100;
private static uint GetLParam(uint scanCode, uint repeatCount = 1, uint extended = 0, uint context = 0,
uint previousState = 0, uint transition = 0)
{
return repeatCount
| (scanCode << 16)
| (extended << 24)
| (context << 29)
| (previousState << 30)
| (transition << 31);
}
[Obsolete("does not work")]
public static void sendkey(IntPtr hWnd)
{
bool retVal = PostMessage(hWnd, WM_KEYDOWN, (IntPtr)'A', (IntPtr)GetLParam((uint)ScanCodeShort.KEY_A, previousState:0, transition:0));
if (!retVal) throw new Exception("PostMessage");
System.Threading.Thread.Sleep(100);
retVal = PostMessage(hWnd, WM_KEYUP, (IntPtr)'A', (IntPtr)GetLParam((uint)ScanCodeShort.KEY_A, previousState: 1, transition: 1));
if (!retVal) throw new Exception("PostMessage");
retVal = PostMessage(hWnd, WM_CHAR, (IntPtr)'A', (IntPtr)GetLParam((uint)ScanCodeShort.KEY_A, previousState: 0, transition: 0));
if (!retVal) throw new Exception("PostMessage");
System.Threading.Thread.Sleep(100);
retVal = PostMessage(hWnd, WM_CHAR, (IntPtr)'A', (IntPtr)GetLParam((uint)ScanCodeShort.KEY_A, previousState: 1, transition: 1));
if (!retVal) throw new Exception("PostMessage");
}
}
}