如何在我的C#windows窗体应用程序中使用sendkey.send方法?

时间:2014-11-14 11:19:20

标签: c# winforms

我已在我的应用程序中编写此代码,我想使用SendKeys.send(" ^ v")将ctrl + v键发送到另一个应用程序,但是当我执行应用程序时它说: SendKeys无法在此应用程序内运行,因为应用程序未处理Windows消息。更改应用程序以处理消息,或使用SendKeys.SendWait方法。 实际上这个应用程序是一个Windows后台应用程序,用于通过按全局键在用户键盘布局错误的情况下转换文本。 我不想通过按ctrl + c并过去使用ctrl + v来获取用户复制文本,我希望在按下globalkey后由app完成此操作,用户必须执行的唯一工作只需选择要转换的文本。 这是我的代码: 有什么帮助吗?

    [DllImport("user32.dll")]   
    public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
    public static void assign()
    {
        var HotKeyManager = new HotkeyManager();
        string s = System.IO.File.ReadAllText(Application.StartupPath + "\\1.dat");
        if (s == "")
            MessageBox.Show("Please go to the setting menu and assign a shortcut keys for EN to Fa.");
        else
        {
            int c = 0;
            for (int i = 0; i < s.Length - 1; i++)
            {
                if (s[i] == 'c')
                    c += Constants.CTRL;
                if (s[i] == 'a')
                    c += Constants.ALT;
                if (s[i] == 's')
                    c += Constants.SHIFT;
            }
            RegisterHotKey(HotKeyManager.Handle, 123, c, s[s.Length - 1]);
        }
    }
    public static void assign2()
    {
        var HotKeyManager = new HotkeyManager();           
        string a = System.IO.File.ReadAllText(Application.StartupPath + "\\2.dat");
        if (a == "")
            MessageBox.Show("Please go to the setting menu and assign a shortcut keys for Fa to En.");
        else
        {
            int x = 0;
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i] == 'c')
                    x += Constants.CTRL;
                if (a[i] == 'a')
                    x += Constants.ALT;
                if (a[i] == 's')
                    x += Constants.SHIFT;
            }
            RegisterHotKey(HotKeyManager.Handle, 234, x, a[a.Length - 1]);
        }
    }
    public static string FaToEn(string[] t)
    {
        string s = "";
        int i;
        for (i = 0; i < t.Length; i++)
            switch (t[i])
            {
                case "ض":
                    {
                        s += "q";
                        break;
                    }
                case "ص":
                    {
                        s += "w";
                        break;
                    }
                case "ث":
                    {
                        s += "e";
                        break;
                    }
                case "ق":
                    {
                        s += "r";
                        break;
                    }
                case "ف":
                    {
                        s += "t";
                        break;
                    }
                case "غ":
                    {
                        s += "y";
                        break;
                    }
                case "ع":
                    {
                        s += "u";
                        break;
                    }
                case "ه":
                    {
                        s += "i";
                        break;
                    }
                case "خ":
                    {
                        s += "o";
                        break;
                    }
                case "ح":
                    {
                        s += "p";
                        break;
                    }
                case "ج":
                    {
                        s += "[";
                        break;
                    }
                case "چ":
                    {
                        s += "]";
                        break;
                    }
                case "ش":
                    {
                        s += "a";
                        break;
                    }
                case "س":
                    {
                        s += "s";
                        break;
                    }
                case "پ":
                    {
                        s += @"\";
                        break;
                    }
                case "ی":
                    {
                        s += "d";
                        break;
                    }
                case "ب":
                    {
                        s += "f";
                        break;
                    }
                case "ل":
                    {
                        s += "g";
                        break;
                    }
                case "ا":
                    {
                        s += "h";
                        break;
                    }
                case "ت":
                    {
                        s += "j";
                        break;
                    }
                case "ن":
                    {
                        s += "k";
                        break;
                    }
                case "آ":
                    {
                        s += "H";
                        break;
                    }
                case "م":
                    {
                        s += "l";
                        break;
                    }
                case "ک":
                    {
                        s += ";";
                        break;
                    }
                case "گ":
                    {
                        s += "'";
                        break;
                    }
                case "ظ":
                    {
                        s += "z";
                        break;
                    }
                case "ط":
                    {
                        s += "x";
                        break;
                    }
                case "ز":
                    {
                        s += "c";
                        break;
                    }
                case "ژ":
                    {
                        s += "C";
                        break;
                    }
                case "ر":
                    {
                        s += "v";
                        break;
                    }
                case "ذ":
                    {
                        s += "b";
                        break;
                    }
                case "د":
                    {
                        s += "n";
                        break;
                    }
                case "ئ":
                    {
                        s += "m";
                        break;
                    }
                case "و":
                    {
                        s += ",";
                        break;
                    }
                case "ً":
                    {
                        s += "Q";
                        break;
                    }
                case "ٌ":
                    {
                        s += "W";
                        break;
                    }
                case "ٍ":
                    {
                        s += "E";
                        break;
                    }
                case "ريال":
                    {
                        s += "R";
                        break;
                    }
                case "،":
                    {
                        s += "T";
                        break;
                    }
                case "؛":
                    {
                        s += "Y";
                        break;
                    }
                case ",":
                    {
                        s += "U";
                        break;
                    }
                case "]":
                    {
                        s += "I";
                        break;
                    }
                case "[":
                    {
                        s += "O";
                        break;
                    }
                case @"\":
                    {
                        s += "P";
                        break;
                    }
                case "ۀ":
                    {
                        s += "G";
                        break;
                    }
                case "ـ":
                    {
                        s += "J";
                        break;
                    }
                case "«":
                    {
                        s += "K";
                        break;
                    }
                case "»":
                    {
                        s += "L";
                        break;
                    }
                case "ة":
                    {
                        s += "Z";
                        break;
                    }
                case "ي":
                    {
                        s += "X";
                        break;
                    }
                case "ؤ":
                    {
                        s += "V";
                        break;
                    }
                case "إ":
                    {
                        s += "B";
                        break;
                    }
                case "أ":
                    {
                        s += "N";
                        break;
                    }
                case "ء":
                    {
                        s += "M";
                        break;
                    }
                case "؟":
                    {
                        s += "?";
                        break;
                    }
                default:
                    {
                        s += t[i];
                        break;
                    }
            }
        return s;
    }
    public static string EnToFa(string[] t)
    {
        string s = "";
        int i;
        for (i = 0; i < t.Length; i++)
            switch (t[i])
            {
                case "q":
                    {
                        s += "ض";
                        break;
                    }
                case "w":
                    {
                        s += "ص";
                        break;
                    }
                case "e":
                    {
                        s += "ث";
                        break;
                    }
                case "r":
                    {
                        s += "ق";
                        break;
                    }
                case "t":
                    {
                        s += "ف";
                        break;
                    }
                case "y":
                    {
                        s += "غ";
                        break;
                    }
                case "u":
                    {
                        s += "ع";
                        break;
                    }
                case "i":
                    {
                        s += "ه";
                        break;
                    }
                case "o":
                    {
                        s += "خ";
                        break;
                    }
                case "p":
                    {
                        s += "ح";
                        break;
                    }
                case "[":
                    {
                        s += "ج";
                        break;
                    }
                case "]":
                    {
                        s += "چ";
                        break;
                    }
                case "a":
                    {
                        s += "ش";
                        break;
                    }
                case "s":
                    {
                        s += "س";
                        break;
                    }
                case @"\":
                    {
                        s += "پ";
                        break;
                    }
                case "d":
                    {
                        s += "ی";
                        break;
                    }
                case "f":
                    {
                        s += "ب";
                        break;
                    }
                case "g":
                    {
                        s += "ل";
                        break;
                    }
                case "h":
                    {
                        s += "ا";
                        break;
                    }
                case "j":
                    {
                        s += "ت";
                        break;
                    }
                case "k":
                    {
                        s += "ن";
                        break;
                    }
                case "H":
                    {
                        s += "آ";
                        break;
                    }
                case "l":
                    {
                        s += "م";
                        break;
                    }
                case ";":
                    {
                        s += "ک";
                        break;
                    }
                case "'":
                    {
                        s += "گ";
                        break;
                    }
                case "z":
                    {
                        s += "ظ";
                        break;
                    }
                case "x":
                    {
                        s += "ط";
                        break;
                    }
                case "c":
                    {
                        s += "ز";
                        break;
                    }
                case "C":
                    {
                        s += "ژ";
                        break;
                    }
                case "v":
                    {
                        s += "ر";
                        break;
                    }
                case "b":
                    {
                        s += "ذ";
                        break;
                    }
                case "n":
                    {
                        s += "د";
                        break;
                    }
                case "m":
                    {
                        s += "ئ";
                        break;
                    }
                case ",":
                    {
                        s += "و";
                        break;
                    }
                case "Q":
                    {
                        s += "ً";
                        break;
                    }
                case "W":
                    {
                        s += "ٌ";
                        break;
                    }
                case "E":
                    {
                        s += "ٍ";
                        break;
                    }
                case "R":
                    {
                        s += "ريال";
                        break;
                    }
                case "T":
                    {
                        s += "،";
                        break;
                    }
                case "Y":
                    {
                        s += "؛";
                        break;
                    }
                case "U":
                    {
                        s += ",";
                        break;
                    }
                case "I":
                    {
                        s += "]";
                        break;
                    }
                case "O":
                    {
                        s += "[";
                        break;
                    }
                case "P":
                    {
                        s += @"\";
                        break;
                    }
                case "{":
                    {
                        s += "}";
                        break;
                    }
                case "A":
                    {
                        s += "َ";
                        break;
                    }
                case "S":
                    {
                        s += "ُ";
                        break;
                    }
                case "D":
                    {
                        s += "ِ";
                        break;
                    }
                case "F":
                    {
                        s += "ّ";
                        break;
                    }
                case "G":
                    {
                        s += "ۀ";
                        break;
                    }
                case "J":
                    {
                        s += "ـ";
                        break;
                    }
                case "K":
                    {
                        s += "«";
                        break;
                    }
                case "L":
                    {
                        s += "»";
                        break;
                    }
                case "Z":
                    {
                        s += "ة";
                        break;
                    }
                case "X":
                    {
                        s += "ي";
                        break;
                    }
                case "V":
                    {
                        s += "ؤ";
                        break;
                    }
                case "B":
                    {
                        s += "إ";
                        break;
                    }
                case "N":
                    {
                        s += "أ";
                        break;
                    }
                case "M":
                    {
                        s += "ء";
                        break;
                    }
                case "?":
                    {
                        s += "؟";
                        break;
                    }
                default:
                    {
                        s += t[i];
                        break;
                    }
            }
        return s;
    }

    public sealed class HotkeyManager : NativeWindow, IDisposable
    {
        public HotkeyManager()
        {
            CreateHandle(new CreateParams());
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Constants.WM_HOTKEY_MSG_ID)
            {
                if (m.WParam.ToInt32() == 123)
                {
                    SendKeys.SendWait("^c");
                    string s = System.Windows.Forms.Clipboard.GetText(TextDataFormat.UnicodeText);
                    int tlength = s.Length;
                    char[] ss = s.ToCharArray();
                    if (tlength > 1)
                    {
                        string[] t = new string[tlength];
                        for (int i = 0; i < tlength; i++)
                            t[i] = ss[i].ToString();
                        System.Windows.Forms.Clipboard.SetText(EnToFa(t), TextDataFormat.UnicodeText);
                    }                        
                }
                if (m.WParam.ToInt32() == 234)
                {
                    SendKeys.SendWait("^c");
                    string s = System.Windows.Forms.Clipboard.GetText(TextDataFormat.UnicodeText);
                    int tlength = s.Length;
                    char[] ss = s.ToCharArray();
                    if (tlength > 1)
                    {
                        string[] t = new string[tlength];
                        for (int i = 0; i < tlength; i++)
                            t[i] = ss[i].ToString();
                        System.Windows.Forms.Clipboard.SetText(FaToEn(t), TextDataFormat.UnicodeText);                            
                    }                        
                }                    
            }
            SendKeys.Send("^v");
            base.WndProc(ref m);               
        }

        public void Dispose()
        {
            DestroyHandle();
        }
    } 

1 个答案:

答案 0 :(得分:0)

您可以尝试以下

1-激活您在后台运行的另一个应用程序

您可能会发现此链接有用FindWindow and SetForegroundWindow alternatives?

2-应用sendKeys

SendKeys.Send("^(V)");

3-重新激活您的主要应用

希望这会对你有所帮助