Autor Beitrag
C#
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Fr 21.02.14 23:58 
Hey @ll,

ich möchte mittels Clipboard Texte in beliebige Textfelder kopieren. So sieht die Funktion aus:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
  private void InsertText(string text)
    {
      string oldContent = Clipboard.GetText();
      Clipboard.SetText(text);
      SendKeys.SendWait("^{v}");
      Clipboard.SetText(oldContent);
    }

Was nun nicht funktioniert wie's soll ist das pasting. Ich habe eine Testform mit einer Textbox. Wenn ich nun die Methode aufrufe (Textbox besitzt fokus) passiert nix. Wenn ich den Explorer öffne und die Navigationsleiste fokussiere, wird der Text beim 1. Aufruf der Methode erfolgreich eingefügt. Aber jedes weitere mal klappt nicht...

Ich verbinde das ganze mit Global Keyboard Hook, aber da funktioniert alles so wie es soll (hab's getestet).

Ich habe auch schon "^(V)" "^V" "^v" ... ausprobiert, aber immer das gleiche Ergebnis.

Hat jemand ne Idee wo der Fehler liegen könnte???

EDIT
Meine Tastenkombi für die Funktion ist Ctrl+Alt+I, nich Ctrl+V oder Ctrl+C falls jemand da den Fehler sucht.

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19315
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 22.02.14 10:17 
Warum schickst du denn nicht gleich den echten Text statt nur die Tastenkombination? Dann brauchst du auch den Weg über die Zwischenablage nicht.
C# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Sa 22.02.14 13:15 
:autsch: Oh mann wie dumm der Mensch doch sein kann :autsch: Danke.

Aber jetzt wird das ganze noch verrückter ?!?!? Wenn ich es jetzt mit SendKeys direkt probiere, wird der string nur 1x erfolgreich übertragen, nachdem ich den Fokus auf eine andere Anwendung wechsle.
Also bei meiner Testform kommt immer "€€€" an und sobald ich z.B. in den Explorer gehe kommt beim 1x "test erfolgreich" und bei jedem weiteren mal (auch wenn Fokus gewechselt wird) "€€€".
Ich hab keinen Plan woher das kommt...

EDIT
Hier der Code der Form
ausblenden volle Höhe C#-Quelltext
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:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
  public partial class Form1 : Form
  {
    private GlobalKeyboardHook2 keyboardHook;
    private ShortcutManager shortcutManager;

    public Form1()
    {
      InitializeComponent();

      keyboardHook = new GlobalKeyboardHook2();
      keyboardHook.HookAllKeys = true;

      //keyboardHook.HookedKeys.Add(Keys.LMenu);
      //keyboardHook.HookedKeys.Add(Keys.LControlKey);
      //keyboardHook.HookedKeys.Add(Keys.I);

      keyboardHook.KeyDown += HookedKeyUpDown;
      keyboardHook.KeyDown += HookedKeyDown;
      keyboardHook.KeyUp += HookedKeyUpDown;

//          HookManager.KeyDown += HookedKeyDown;

      shortcutManager = new ShortcutManager();
      shortcutManager.AddShortcut(new Shortcut(new List<Keys> {Keys.LControlKey, Keys.LMenu}, new List<Keys> {Keys.I}));
      shortcutManager.OnShortcutComplete += ShortcutComplete;
    }

    private void HookedKeyUpDown(object sender, KeyEventArgs e)
    {
      shortcutManager.Update(keyboardHook.GetPressedKeys());
    }

    private void ShortcutComplete(object sender, ShortcutEventArgs e)
    {
      InsertText("test erfolgreich");
    }

    private void HookedKeyDown(object sender, KeyEventArgs e)
    {
      string s = "";
      if (e.Control)
        s += "Control + ";
      if (e.Alt)
        s += "Alt + ";
      if (e.Shift)
        s += "Shift + ";
      s += e.KeyCode.ToString();

      boxHisotry.AppendText(s + Environment.NewLine);
    }

    private void InsertText(string text)
    {
      //string oldContent = Clipboard.GetText();
      //Clipboard.SetText(text);
      SendKeys.Send(text);
      //Clipboard.SetText(oldContent);
    }
  }

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler
C# Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 561
Erhaltene Danke: 65

Windows 10, Kubuntu, Android
Visual Studio 2017, C#, C++/CLI, C++/CX, C++, F#, R, Python
BeitragVerfasst: Mo 24.02.14 16:45 
Hmm komisch. Ich hab das ganze jetzt noch ein paar mal mit variablen statt hardcode string gemacht und nun klappt das Einfügen auch wie es soll :?

_________________
Der längste Typ-Name im .NET-Framework ist: ListViewVirtualItemsSelectionRangeChangedEventHandler