Autor Beitrag
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: So 31.01.10 21:47 
Ctrl hatte ich vorher zwar auch schon gesehen, wusste aber ehrlich gesagt nicht wie ich damit umgehen soll. Ich hoffe mal, dass ich das jetzt richtig angewendet habe.

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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ManagedWinapi;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Hotkey hk1 = new Hotkey();
            hk1.Ctrl = true;
            hk1.KeyCode = Keys.Control | Keys.A;
            hk1.HotkeyPressed += new EventHandler(hk1_HotkeyPressed);
        }

        void hk1_HotkeyPressed(object sender, EventArgs e)
        {
            MessageBox.Show("geht");
        }


    }
}


Naja, hätte ich es richtig verwendet, würde es jetzt funktionieren :D
Also ich bekomme zwar nach wie vor keine Fehlermeldung, jedoch wird mir auch keine Messagebox ausgegeben.
Ich geb zu das ich mich ein wenig ungeschickt anstelle, seht mir das bitte nach ^^
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 31.01.10 21:55 
user profile iconChristian S. hat folgendes geschrieben Zum zitierten Posting springen:
Da gibt es z.B. die Eigenschaft Enabled.


(Und das Control dann natürlich aus dem Keycode rauslassen)

Ctrl+A könnte außerdem schon belegt sein ("Alles markieren")

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: So 31.01.10 22:23 
So wie der Code da steht, passiert garnichts wenn ich die tasten drücke.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
private void Form1_Load(object sender, EventArgs e)
        {
            Hotkey hk1 = new Hotkey();
            hk1.Ctrl = true;
            hk1.KeyCode = Keys.Control | Keys.A;
            hk1.HotkeyPressed += new EventHandler(hk1_HotkeyPressed);
            hk1.Enabled = true;
        }


Folgendes: Ich habe | einmal durch & ersetzt und siehe da, es funktioniert fasst. Aber ebend nur fasst. Das Programm reagiert nämlich nicht auf die Kombination, sondern nur auf Strg. Ich habe einfach mal aus interesse folgende Zeile entfernt:

ausblenden C#-Quelltext
1:
hk1.KeyCode = Keys.Control | Keys.A;					


Und immer noch funktioniert Strg. Es scheint fasst so, als wenn die Zeile überhaupt nicht beachtet werden würde. Eher als ob nur auf hk1.Ctrl = true; geachtet würd. Was mach ich jetzt schonwieder falsch :D?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 31.01.10 22:31 
:autsch:
user profile iconChristian S. hat folgendes geschrieben Zum zitierten Posting springen:
(Und das Control dann natürlich aus dem Keycode rauslassen)


Und jetzt antworte ich nicht mehr, das ist mir zu doof.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: So 31.01.10 22:37 
Tut mir leid, ich hatte gerade ein Brett vorm Kopf :D

Es funktioniert jetzt zumindest. Vielen Dank an alle^^

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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ManagedWinapi;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Hotkey hk1 = new Hotkey();
            hk1.Ctrl = true;
            hk1.KeyCode = Keys.A;
            hk1.HotkeyPressed += new EventHandler(hk1_HotkeyPressed);
            hk1.Enabled = true;
        }
        private void hk1_HotkeyPressed(object sender, EventArgs e)
        {
            MessageBox.Show("geht");
        }
    }
}
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Sa 30.10.10 11:50 
Tut mir leid das ich das alte Thema nochmal raushole aber ich habe gerade ein altes Projekt von mir über die Managed Winapi wiedergefunden.
Damals hatte ich es als Unterstützungstool für Spiele geschrieben.
Der Sinn war: Ich drücke z.B. die Taste "g" und mein Tool sendet 3x "g" zurück. Man konnte einstellen welche Taste wie oft betätigt werden soll.
Unter Win Xp lief das auch alles. Jetzt wollte ich ebend mein Programm unter Win7 testen habe ein Problem festgestellt bei dem ich echt nicht weiter weiß :D

Unter Xp hat das funktioniert:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public int Anzahl;
private void Form1_Load(object sender, EventArgs e)
{
     Hotkey hka = new Hotkey();
     hka.Enabled = true;
     hka.KeyCode = Keys.A;
     hka.HotkeyPressed += new EventHandler(hka_HotkeyPressed);
}

void hka_HotkeyPressed(object sender, EventArgs e)
{
    int i = 0;
    while(Anzahl != i)
    {     
         SendKeys.Send("a");
         i++;
    }
}


Unter Xp war es scheinbar so, dass SendKeys nicht vom hka_HotkeyPressed Event abgefangen wurden.
Unter Win7 ist das leider so. Wenn ich per SendKey einen Buchstaben schicke, wird der vom Event abgefangen. Dass führt in meinem Code zu einer Unendlichkeitsschleife.
Was kann ich jetzt dagegen tun? Auf andere Buchstaben möchte ich nicht ausweichen, alleine schon aus Lernzwecken. Es müsste doch einen Weg geben, dass Sendkeys nicht vom Event abegangen wird oder?
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 30.10.10 13:52 
Benutze ein Klassenfeld als Flag, das angibt, ob der Eventhandler gerade durchlaufen wird. Wird die Methode betreten und das Flag ist true, weißt du, dass sie rekursiv von Send aufgerufen wurde.
Also das Standardmittel gegen Reentrancy.

_________________
>λ=
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Sa 30.10.10 15:59 
Ich habe gerade nach Flags gegoogled und rausgefunden, dass es im Zusammenhang mit Enums, also Aufzählungen, steht.
Ich begreife ab er nicht wie mir Aufzählungen jetzt weiterhelfen sollen. Ausserdem ist mir nicht klar wie ich von Flags etwas true oder false zurück bekommen.
Ich wäre dir sehr dankbar, wenn du dazu etwas mehr erklären könntest weil damit komme ich jetzt nicht weiter :D
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 30.10.10 16:47 
Ein "Flag" bezeichnet erst einmal einfach eine boolesche Variable ;) . Nennen wir sie von mir aus bool isHotKeyPressedExecuted.

_________________
>λ=
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Sa 30.10.10 18:38 
Ich hab vorhin, bevor ich den Thread eröffnet habe, folgendes ausprobiert (alles unwichtige habe ich jetzt entfernt):

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
public static bool send;
void hka_HotkeyPressed(object sender, EventArgs e)
{
      if (send == false)
      { 
           SendKeys.Send("a"); 
           send = true
      }
      else 
      {
           send = false;
      }
}


Dadurch wird die "Schleife" zwar unterbrochen, es wird aber kein Buchstabe an die Anwendung geschickt. Der Buchstabe löst einfach wieder das Event aus. Es ist falsch umgesetzt aber ist der Ansatz mit bool richtig?
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Fr 05.11.10 18:04 
Hat sonst jemand eine Idee?
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 05.11.10 18:49 
So müsste es richtig gehen:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
 if (!send) // besser als == false! -)
 {
     send = true;
     SendKeys.Send("a");
     send = false;
 }

So wird erst 'send' gesetzt und falls SendKeys wieder aufgerufen wird, passiert dann nichts, da ja mittels "!send" dies abgefangen wird.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 05.11.10 18:49 
ausblenden C#-Quelltext
1:
2:
           SendKeys.Send("a"); 
           send = true;


Einfach mal die beiden Befehle drehen. Wenn das hilft sollte dir klar sein warum.
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Mo 08.11.10 22:33 
Ok, wieso die Befehle gedreht theoretisch funktionieren sollten ist mir klar, es funktioniert aber leider trotzdem nicht.
Ich glaube so allmählich diese Herangehensweise ist komplett falsch denn der SendKeys-Befehl wird immer vom Event abgefangen. Durch den Code entsteht zwar keine Schleife mehr aber sobald "a" gesendet wird, fängt das Event den Buchstaben sofort ab bevor irgendeine andere Anwendung überhaupt etwas von dem "a" mitbekommet :D

Ich habe ebend meine Benutzerkontensteuerung mal ausgestellt weil ich es einfach nicht verstehe, dass mein Code unter Xp noch funktioniert hat. Mit ausgeschalteter Benutzerkontensteuerung funktioniert der Code seltsamerweise auch. Aber das ist keine Lösung. Allerding sagt das ja vielleich einem etwas :D?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 08.11.10 22:44 
Alternativ könntest du auch mal probieren in deinen HotkeyPressed Event den Hotkey einfach vor dem SendKey abzuschalten(über die Enabled Property der HotKey Klasse) und nachher wieder an.
Ccenter Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Di 09.11.10 19:17 
Na super, darauf bin ich natürlich gar nicht erst gekommen :D
Vielen Dank, damit sollte ich mein Problem lösen können.