* SendKeys.cs: window handle is not needed in win32, so just
authorAndreia Gaita <avidigal@novell.com>
Wed, 3 Jan 2007 16:18:00 +0000 (16:18 -0000)
committerAndreia Gaita <avidigal@novell.com>
Wed, 3 Jan 2007 16:18:00 +0000 (16:18 -0000)
get the active window for X after parsing keys and don't use
it when building the message; it is passed by parameter to the
Xplat method and used there to build the message instead. Also,
wait for events to be processed on SendWait, as opposed to Send,
which doesn't wait :) Playing with threads and Send() completely
hangs on ms.net, only SendWait() works.

XplatUIX11.cs
X11Display.cs: Check for valid window handle.

2007-01-03  Andreia Gaita  <avidigal@novell.com>

svn path=/trunk/mcs/; revision=70425

mcs/class/Managed.Windows.Forms/System.Windows.Forms.X11Internal/ChangeLog
mcs/class/Managed.Windows.Forms/System.Windows.Forms.X11Internal/X11Display.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/System.Windows.Forms/SendKeys.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUIX11.cs

index 89f58d31f1f883bbd25fb096be1d608ea2361ed5..8318ebef9552c7f8f3c0b966ef3d3346338fbe44 100644 (file)
@@ -1,3 +1,8 @@
+2007-01-03  Andreia Gaita  <avidigal@novell.com>
+       
+       XplatUIX11.cs (see main changelog)
+       X11Display.cs: Check for valid window handle.
+
 2006-12-28  Rolf Bjarne Kvinge  <RKvinge@novell.com>
 
        * X11Display.cs:
index 14c01f221c033790cba7dc6cb240b07cf3b7dfd8..3d9ac2eecc8673f5a282ffdb3e412281ef5dc03b 100644 (file)
@@ -556,14 +556,17 @@ namespace System.Windows.Forms.X11Internal {
                }
 
                public int SendInput (IntPtr handle, Queue keys) {
+                       if (handle == IntPtr.Zero)
+                               return 0;
+
                        int count = keys.Count;
+                       Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
 
                        while (keys.Count > 0) {
                        
                                MSG msg = (MSG)keys.Dequeue();
 
                                XEvent xevent = new XEvent ();
-                               Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
 
                                xevent.type = (msg.message == Msg.WM_KEYUP ? XEventName.KeyRelease : XEventName.KeyPress);
                                xevent.KeyEvent.display = display;
index b4f91e56c4cbfd111eb0e29e9304521e0620e924..d130221eb2e70df6cc257eb3a8a7cb25b254797a 100644 (file)
@@ -1,3 +1,16 @@
+2007-01-03  Andreia Gaita  <avidigal@novell.com>
+
+       * SendKeys.cs: window handle is not needed in win32, so just
+       get the active window for X after parsing keys and don't use
+       it when building the message; it is passed by parameter to the 
+       Xplat method and used there to build the message instead. Also,
+       wait for events to be processed on SendWait, as opposed to Send,
+       which doesn't wait :) Playing with threads and Send() completely 
+       hangs on ms.net, only SendWait() works.
+       
+       XplatUIX11.cs
+       X11Display.cs: Check for valid window handle.
+
 2007-01-03  Jackson Harper  <jackson@ximian.com>
 
        * TextControl.cs: Need to prevent wrap calculations when replacing
index 5e9d14ecb1e2578b732c9b69a40ec4a6f5d9606c..0c700f892fa82b0c5895e84100ed31af6ae9077e 100644 (file)
@@ -25,8 +25,6 @@
 //
 //
 
-// COMPLETE
-
 using System.Collections;
 using System.Runtime.InteropServices;
 using System.Text;
@@ -47,7 +45,6 @@ namespace System.Windows.Forms {
                #region Local variables
                private static Queue keys = new Queue();
                private static Hashtable keywords;
-               private static IntPtr hwnd;
                #endregion
 
                static SendKeys() {
@@ -109,30 +106,27 @@ namespace System.Windows.Forms {
                }
 
 
-               private static void AddVKey(IntPtr hwnd, int vk, bool down) 
+               private static void AddVKey(int vk, bool down) 
                {
                        MSG msg = new MSG();
-                       msg.hwnd = hwnd;
                        msg.message = down ? Msg.WM_KEYDOWN : Msg.WM_KEYUP;
                        msg.wParam = new IntPtr(vk);
                        msg.lParam = IntPtr.Zero;
                        keys.Enqueue(msg);
                }
 
-               private static void AddVKey(IntPtr hwnd, int vk, int repeat_count) 
+               private static void AddVKey(int vk, int repeat_count) 
                {
                        MSG     msg;
 
                        for (int i = 0; i < repeat_count; i++ ) {
                                msg = new MSG();
-                               msg.hwnd = hwnd;
                                msg.message = Msg.WM_KEYDOWN;
                                msg.wParam = new IntPtr(vk);
                                msg.lParam = (IntPtr)1;
                                keys.Enqueue(msg);
 
                                msg = new MSG();
-                               msg.hwnd = hwnd;
                                msg.message = Msg.WM_KEYUP;
                                msg.wParam = new IntPtr(vk);
                                msg.lParam = IntPtr.Zero;
@@ -141,19 +135,17 @@ namespace System.Windows.Forms {
                        }
                }
 
-               private static void AddKey(IntPtr hwnd, char key, int repeat_count) {
+               private static void AddKey(char key, int repeat_count) {
                        MSG     msg;
 
                        for (int i = 0; i < repeat_count; i++ ) {
                                msg = new MSG();
-                               msg.hwnd = hwnd;
                                msg.message = Msg.WM_KEYDOWN;
                                msg.wParam = new IntPtr(key);
                                msg.lParam = IntPtr.Zero;
                                keys.Enqueue(msg);
 
                                msg = new MSG();
-                               msg.hwnd = hwnd;
                                msg.message = Msg.WM_KEYUP;
                                msg.wParam = new IntPtr(key);
                                msg.lParam = IntPtr.Zero;
@@ -170,7 +162,6 @@ namespace System.Windows.Forms {
                        bool isCtrl = false;
                        bool isAlt = false;
 
-                       hwnd = ((Form)Control.FromHandle(XplatUI.GetActive())).ActiveControl.Handle;
                        StringBuilder repeats = new StringBuilder();
                        StringBuilder group_string = new StringBuilder();
                        
@@ -218,50 +209,50 @@ namespace System.Windows.Forms {
                                                if (repeats.Length > 0)
                                                        repeat = Int32.Parse(repeats.ToString());
                                                if (isVkey)
-                                                       AddVKey(hwnd, (int)keywords[group_string.ToString().ToUpper()], repeats.Length == 0 ? 1 : repeat);
+                                                       AddVKey((int)keywords[group_string.ToString().ToUpper()], repeats.Length == 0 ? 1 : repeat);
                                                else {
                                                        if (Char.IsUpper(Char.Parse(group_string.ToString()))) {
                                                                if (!isShift)
-                                                                       AddVKey(hwnd, (int)keywords["+"], true);
-                                                               AddKey(hwnd, Char.Parse(group_string.ToString()), 1);
+                                                                       AddVKey((int)keywords["+"], true);
+                                                               AddKey(Char.Parse(group_string.ToString()), 1);
                                                                if (!isShift)
-                                                                       AddVKey(hwnd, (int)keywords["+"], false);
+                                                                       AddVKey((int)keywords["+"], false);
                                                        }
                                                        else
-                                                               AddKey(hwnd, Char.Parse(group_string.ToString().ToUpper()), repeats.Length == 0 ? 1 : repeat);
+                                                               AddKey(Char.Parse(group_string.ToString().ToUpper()), repeats.Length == 0 ? 1 : repeat);
                                                }
 
                                                i = start;
                                                isRepeat = isKey = isVkey = false;
                                                if (isShift)
-                                                       AddVKey(hwnd, (int)keywords["+"], false);
+                                                       AddVKey((int)keywords["+"], false);
                                                if (isCtrl)
-                                                       AddVKey(hwnd, (int)keywords["^"], false);
+                                                       AddVKey((int)keywords["^"], false);
                                                if (isAlt)
-                                                       AddVKey(hwnd, (int)keywords["%"], false);
+                                                       AddVKey((int)keywords["%"], false);
                                                isShift = isCtrl = isAlt = false;
                                                break;
                                        
                                        case '+': {
-                                               AddVKey(hwnd, (int)keywords["+"], true);
+                                               AddVKey((int)keywords["+"], true);
                                                isShift = true;;
                                                break;
                                        }
 
                                        case '^': {
-                                               AddVKey(hwnd, (int)keywords["^"], true);
+                                               AddVKey((int)keywords["^"], true);
                                                isCtrl = true;
                                                break;
                                        }
 
                                        case '%': {
-                                               AddVKey(hwnd, (int)keywords["%"], true);
+                                               AddVKey((int)keywords["%"], true);
                                                isAlt = true;
                                                break;
                                        }
 
                                        case '~': {
-                                               AddVKey(hwnd, (int)keywords["ENTER"], 1);
+                                               AddVKey((int)keywords["ENTER"], 1);
                                                break;
                                        }
 
@@ -271,11 +262,11 @@ namespace System.Windows.Forms {
 
                                        case ')': {
                                                if (isShift)
-                                                       AddVKey(hwnd, (int)keywords["+"], false);
+                                                       AddVKey((int)keywords["+"], false);
                                                if (isCtrl)
-                                                       AddVKey(hwnd, (int)keywords["^"], false);
+                                                       AddVKey((int)keywords["^"], false);
                                                if (isAlt)
-                                                       AddVKey(hwnd, (int)keywords["%"], false);
+                                                       AddVKey((int)keywords["%"], false);
                                                isShift = isCtrl = isAlt = isBlock = false;
                                                break;
                                        }
@@ -283,21 +274,21 @@ namespace System.Windows.Forms {
                                        default: {
                                                if (Char.IsUpper(key_string[i])) {
                                                        if (!isShift)
-                                                               AddVKey(hwnd, (int)keywords["+"], true);
-                                                       AddKey(hwnd, key_string[i], 1);
+                                                               AddVKey((int)keywords["+"], true);
+                                                       AddKey(key_string[i], 1);
                                                        if (!isShift)
-                                                               AddVKey(hwnd, (int)keywords["+"], false);
+                                                               AddVKey((int)keywords["+"], false);
                                                }
                                                else
-                                                       AddKey(hwnd, Char.Parse(key_string[i].ToString().ToUpper()), 1);
+                                                       AddKey(Char.Parse(key_string[i].ToString().ToUpper()), 1);
                                                
                                                if (!isBlock) {
                                                        if (isShift)
-                                                               AddVKey(hwnd, (int)keywords["+"], false);
+                                                               AddVKey((int)keywords["+"], false);
                                                        if (isCtrl)
-                                                               AddVKey(hwnd, (int)keywords["^"], false);
+                                                               AddVKey((int)keywords["^"], false);
                                                        if (isAlt)
-                                                               AddVKey(hwnd, (int)keywords["%"], false);
+                                                               AddVKey((int)keywords["%"], false);
                                                        isShift = isCtrl = isAlt = isBlock = false;
                                                }
                                                break;
@@ -309,29 +300,40 @@ namespace System.Windows.Forms {
                                throw new ArgumentException("SendKeys string {0} is not valid.", key_string);
 
                        if (isShift)
-                               AddVKey(hwnd, (int)keywords["+"], false);
+                               AddVKey((int)keywords["+"], false);
                        if (isCtrl)
-                               AddVKey(hwnd, (int)keywords["^"], false);
+                               AddVKey((int)keywords["^"], false);
                        if (isAlt)
-                               AddVKey(hwnd, (int)keywords["%"], false);
+                               AddVKey((int)keywords["%"], false);
+
+               }
 
+               private static void SendInput() {
+                       IntPtr hwnd = IntPtr.Zero;
+                       if (XplatUI.GetActive() != IntPtr.Zero)
+                               hwnd = ((Form)Control.FromHandle(XplatUI.GetActive())).ActiveControl.Handle;
+
+                       XplatUI.SendInput(hwnd, keys);
+                       keys.Clear();
                }
 
                #endregion      // Private Methods
 
                #region Public Static Methods
                public static void Flush() {
-                       XplatUI.SendInput(hwnd, keys);
-                       keys.Clear();
+                       Application.DoEvents();
                }
 
                public static void Send(string keys) {
                        Parse(keys);
-                       Flush();
+                       SendInput();
                }
 
+               private static object lockobj = new object();
                public static void SendWait(string keys) {
-                       Parse(keys);
+                       lock(lockobj) {
+                               Send(keys);
+                       }
                        Flush();
                }
 
index b306f4c5018d7edcde4628c13f1cac86f1a05810..f4cd9aea3288d91182f07463fd9f6ebe4af4b6ee 100644 (file)
@@ -4391,14 +4391,17 @@ namespace System.Windows.Forms {
                }
 
                internal override int SendInput(IntPtr handle, Queue keys) { 
+                       if (handle == IntPtr.Zero)
+                               return 0;
+
                        int count = keys.Count;
+                       Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
 
                        while (keys.Count > 0) {
                        
                                MSG msg = (MSG)keys.Dequeue();
 
                                XEvent xevent = new XEvent ();
-                               Hwnd hwnd = Hwnd.ObjectFromHandle(handle);
 
                                xevent.type = (msg.message == Msg.WM_KEYUP ? XEventName.KeyRelease : XEventName.KeyPress);
                                xevent.KeyEvent.display = DisplayHandle;