• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4275
  • Last Modified:

how to sendmessage key escape

how to sendmessage key escape in c#

        [DllImport("user32.dll")]
        static extern int SendMessage(long hWnd, int msg, int wParam, int lParam);

        SendMessage(handler, 0x1b, 0, 0);  //? not working

I also used SendInput, but I don't know what's the perfect solution and how to input fields in SendInput;
        public enum VK : ushort { ESCAPE = 0x1B }
        public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public long time; public uint dwExtraInfo; };
        [StructLayout(LayoutKind.Explicit, Size = 28)]
        public struct INPUT { [FieldOffset(0)] public uint type;  [FieldOffset(4)] public KEYBDINPUT ki; };
        [DllImport("user32.dll")]
        public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
0
systan
Asked:
systan
  • 9
  • 4
  • 3
2 Solutions
 
iHadiCommented:
Hi,

Try the following code:

        [DllImport("User32.dll")]
        public static extern IntPtr SendMessage(IntPtr hwnd, int uMsg, int wParam, IntPtr lParam);

        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int VK_ESCAPE = 0x1B;

        private void SendEscape(IntPtr hwnd)
        {
            SendMessage(hwnd, WM_KEYDOWN, VK_ESCAPE, IntPtr.Zero);
            SendMessage(hwnd, WM_KEYUP, VK_ESCAPE, IntPtr.Zero);
        }
0
 
systanAuthor Commented:
hi, I used;

[DllImport("User32.dll")]
        public static extern IntPtr SendMessage(long hwnd, int uMsg, int wParam, IntPtr lParam);

        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int VK_ESCAPE = 0x1B;

        private void SendEscape(long hwnd)
        {
            SendMessage(hwnd, WM_KEYDOWN, VK_ESCAPE, IntPtr.Zero);
            SendMessage(hwnd, WM_KEYUP, VK_ESCAPE, IntPtr.Zero);
        }


my begin code...
SendEscape(handler);
my end code...

no errors, but didn't work, don't know why?
0
 
iHadiCommented:
to check if is working, sow a message box on the keydown event of the window you are passing it's handle.

If the window is not in your project, test it on a form in your project to make sure that the code is working.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AndyAinscowCommented:
Often a problem with pasing keystrokes is that the window being sent to is NOT the window you want the keystrokes sent to.

This might be interesting/relevant to you:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_26984941.html
0
 
systanAuthor Commented:
thank you both
I'll get back
0
 
systanAuthor Commented:
it does work with messagebox
but I don't understand why it isn't workin on my app

guys, what about sendinput
public enum VK : ushort { ESCAPE = 0x1B }
        public struct KEYBDINPUT { public ushort wVk; public ushort wScan; public uint dwFlags; public long time; public uint dwExtraInfo; };
        [StructLayout(LayoutKind.Explicit, Size = 28)]
        public struct INPUT { [FieldOffset(0)] public uint type;  [FieldOffset(4)] public KEYBDINPUT ki; };
        [DllImport("user32.dll")]
        public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

Open in new window


SendInput(whats the fields here?);
0
 
systanAuthor Commented:
>>to check if is working, sow a message box on the keydown event of the window you are passing it's handle.
yes it message a box, but it's not sending an escape key,

how about sendInput
0
 
systanAuthor Commented:
SendKeys.Send("{ESC}"); //is working, but not sendMessage
0
 
iHadiCommented:
Hi

Try PostMessage instead of SendMessage:

        [DllImport("User32.dll")]
        public static extern bool PostMessage(IntPtr hwnd, int uMsg, int wParam, int lParam);

        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int VK_ESCAPE = 0x1B;


        private void SendEscape(IntPtr hwnd)
        {
            PostMessage(hwnd, WM_KEYDOWN, VK_ESCAPE, 0);
            PostMessage(hwnd, WM_KEYUP, VK_ESCAPE, 0);
        }
0
 
AndyAinscowCommented:
OK, so SendKeys - as my link recommended - is working.  
SendKeys is designed to emulate keypresses - in other words it is the correct functionality for what you require.




This (and SendMessage vs PostMessage) was gone into in the link I provided previously.
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_26984941.html
0
 
systanAuthor Commented:
thank you both
I'll get back for some good test.
0
 
systanAuthor Commented:
I haven't tried the SendiNput, because you haven't replied my request, but anyway I can select 1 of the functions to execute escape key, SendKey, SendMessage, PostMessage.

thanks for the inputs.
0
 
AndyAinscowCommented:
SendKeys IS the .net version of SendInput.
0
 
systanAuthor Commented:
Oh, SendInput is from vb6, now SendKeys is the .NET version?!
I understand.

thanks
0
 
AndyAinscowCommented:
SendInput is the windows API, VB6 was not .net so it used the windows API.
0
 
systanAuthor Commented:
Yes, I understand, SendInput is a windows APi, vb6 is using it to perform a sendKey.  Now .NET has SendKey function from .NET frameworks.

thanks
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 9
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now