IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

Hook clavier KeyEventArg : e.Control toujours false [Débutant]


Sujet :

Windows Forms

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2014
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2014
    Messages : 8
    Points : 6
    Points
    6
    Par défaut Hook clavier KeyEventArg : e.Control toujours false
    Bonjour,

    Je travaille sur un projet en stage et j'essaye de mettre en place un KeyLogger complet pour un clavier Allemand afin d'enregistrer les actions d'un utilisateur (l'utilisateur est au courant du fait que ses saisies clavier sont enregistres, cest pour mener une etude)
    Javais une premiere solution qui marchait pas mal avec KeyPress, sauf que je me suis rendu compte il y a quelques jours que je ne recuperais pas les combinaisons du style ctrl + A. Apres quelques recherces, je me suis rendu compte quil fallait en fait que j'utilise les evenements KeyUp et KeyDown pour avoir une solution complete. J'ai donc essaye de mettre en place des flag de type booleen sur Control et Alt dans ma WinForm, en utilisant les attributs de KeyEventArgs Alt, Control, modifiers etc.

    Voici le code de ma WinForm et le code de KeyProc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    private IntPtr KeyProc(int nCode, int wParam, IntPtr lParam)
            {
                bool handled = false;
                //On verifie si tous est ok
                if ((nCode >= 0) && (m_onKeyDown != null || m_onKeyUp != null || m_onKeyPress != null))
                {
                    //Remplissage de la structure KeyboardHookStruct a partir d'un pointeur
                    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
                    //KeyDown
     
     
                    if (m_onKeyDown != null && (wParam == 0x100 || wParam == 0x104))
                    {
                        Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                        KeyEventArgs e = new KeyEventArgs(keyData);
                        m_onKeyDown(this, e);
                        handled = handled || e.Handled;
                    }
     
                    // KeyPress
                    if (m_onKeyPress != null && wParam == 0x100)
                    {
                        // Si la touche Shift est appuyée
                        bool isShift = ((GetKeyState(0x10) & 0x80) == 0x80 ? true : false);
                        // Si la touche CapsLock est appuyée
                        bool isCapslock = (GetKeyState(0x14) != 0 ? true : false);
     
                        //bool isCtrl = (GetKeyState(0x11) != 0 ? true : false);
                        //bool isAlt = (GetKeyState(0x12) != 0 ? true : false);
     
     
     
                        byte[] keyState = new byte[256];
                        GetKeyboardState(keyState);
                        byte[] inBuffer = new byte[2];
                        if (ToAscii(MyKeyboardHookStruct.vkCode,
                                  MyKeyboardHookStruct.scanCode,
                                  keyState,
                                  inBuffer,
                                  MyKeyboardHookStruct.flags) == 1)
                        {
                            char key = (char)inBuffer[0];
                            if ((isCapslock ^ isShift) && Char.IsLetter(key))
                                key = Char.ToUpper(key);
     
                            KeyPressEventArgs e = new KeyPressEventArgs(key);
                            m_onKeyPress(this, e);
                            handled = handled || e.Handled;
                        }
                    }
     
                    // KeyUp
                    if (m_onKeyUp != null && (wParam == 0x101 || wParam == 0x105))
                    {
                        Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
                        KeyEventArgs e = new KeyEventArgs(keyData);
                        m_onKeyUp(this, e);
                        handled = handled || e.Handled;
                    }
     
                }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    private void OnKeyUp(object sender, KeyEventArgs e)
            {
                if (e.Control)
                    flagD = false;
            }
     
            private void OnKeyDown(object sender, KeyEventArgs e)
            {
                textBox1.Text += e.KeyCode;
                flagD = e.Control ? true : false;
                textBox1.Text += " " + flagD;  
            }
    Quand j'appuie sur la touche control, elle s'affiche bien dans la textBox mais pourtant e.Control est toujours faux, je ne comprends pas pourquoi. Mon objectif etant dutiliser un booleen qui detecte si Controle est active dans KeyDown, puis de remettre le flag a false quand la touche est relache (donc detecte dans KeyUp).

    Je suis bien perdu et je bloque depuis plusieurs jours, un peu d'aide serait grandement apprecie.. Je suis encore bien noob en C# j'ai commence il y a un mois et demi donc si il y a un peu de lecture a avoir qui pourrait m'etre utile je suis aussi preneur

    Bonne journee, merci d'avance, Vincent

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2014
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2014
    Messages : 8
    Points : 6
    Points
    6
    Par défaut
    Bonjour,

    Pour ceux qui se poserait eventuellement la question, j'ai resolu le probleme en faisant un systeme de Flag en utilisant les valeurs KeyValue recupere lorsque les evenements OnKeyUp / OnKeyDown sont leves. Par exemple, l'appuie sur la touche Controle de gauche a pour valeur 162. Quand on recupere 162 dans KeyDown, on mets le flagControlLeft a 1; quand on recupere 162 dans KeyUp on mets le flag a 0.

    La classe flags.cs :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace WindowsFormsApplication1
    {
        public class Flags
        {
            /* KeyValue => Modifiers
             * 162 => ControlLeft
             * 163 => ControlRight
             * 164 => Alt
             * 162|165 => AltGr
             * This one is a specific one; indeed AltGr is shortcut for ControlLeft+Alt, therefore you have first 162 OnKeyDown, then 165 OnKeyDown.
             * However, the remaining value is 165 for e.KeyValue (the event has the value of the last key who was down, even if you are holding another one at the same time
             * 
             * */
     
            #region attributs
     
            private bool flagCtrlL;
            private bool flagCtrlR;
            private bool flagAlt;
            private bool flagCtrlAlt;
            private bool flagF;
            private bool flagAltGr;
     
            #endregion
     
            public Flags()
            {
                this.flagAlt = false;
                this.flagAltGr = false;
                this.flagCtrlAlt = false;
                this.flagCtrlL = false;
                this.flagCtrlR = false;
                this.flagF = false;
            }
     
            #region methode
     
            public void MappingF(int keyValue)
            {
                if ((keyValue == 112) || (keyValue == 113) || (keyValue == 114) || (keyValue == 115) || (keyValue == 116) || (keyValue == 117) || ((keyValue == 118) || (keyValue == 119) || (keyValue == 120) || (keyValue == 121) || (keyValue == 122) || (keyValue == 123))) //F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12
                    flagF = true;
            }
     
     
            public void UnMappingF(int keyValue)
            {
                if ((keyValue == 112) || (keyValue == 113) || (keyValue == 114) || (keyValue == 115) || (keyValue == 116) || (keyValue == 117) || ((keyValue == 118) || (keyValue == 119) || (keyValue == 120) || (keyValue == 121) || (keyValue == 122) || (keyValue == 123))) //F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12
                    flagF = false;
            }
     
            public void MappingModifiers(int keyValue)    //set the flags depending on KeyValue; every specific KeyValue used beloy corresponds to a modifier; please refer to summary constant for all value
            {
                switch(keyValue)
                {  
                    case 162:                   //keyValue for CtrlL
                        this.flagCtrlL = true;
                        break;
     
                    case 163:                   //keyValue for CtrlR
                        this.flagCtrlR = true;
                        break;
     
                    case 164:                   //keyValue for Alt
                        this.flagAlt = true;
                        break;
     
                    case 165:                   //keyValue for AltGr
                        this.flagAltGr = true;
                        break;
     
                    default:
                        break;
                }
     
                if ((this.flagAlt && this.flagCtrlL == true) || (this.flagAlt && this.flagCtrlR == true))
                    this.flagCtrlAlt = true;
            }
     
     
            public void UnMappingModifiers(int keyValue)
            {
                switch (keyValue)
                {
                    case 162:                   //keyValue for CtrlL
                        this.flagCtrlL = false;
                        break;
     
                    case 163:                   //keyValue for CtrlR
                        this.flagCtrlR = false;
                        break;
     
                    case 164:                   //keyValue for Alt
                        this.flagAlt = false;
                        break;
     
                    case 165:                   //keyValue for AltGr
                        this.flagAltGr = false;
                        break;
     
                    default:
                        break;
                }
                if ((this.FlagCtrlL == false) || (this.FlagCtrlR == false) || (this.FlagAlt == false))
                    this.FlagCtrlAlt = false;
            }
     
     
            public bool WriteControlLeft(int keyValue)
            {
                if (this.flagCtrlL == true && (this.flagAlt == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false)) //only write down if the user is not making a combination
                    if (keyValue != 162)        //if the user holds CtrlL, we don't want to write it
                        return true;
                return false;
            }
     
            public bool WriteControlRight(int keyValue)
            {
                if (this.flagCtrlR == true && (this.flagAlt == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false)) //only write down if the user is not making a combination
                    if (keyValue != 163)       //if the user holds CtrlR, we don't want to write it until keydown is another character
                        return true;
                return false;
            }
     
            public bool WriteAlt(int keyValue)
            {
                if (this.flagAlt == true && (this.flagCtrlL == false) && (this.flagCtrlR == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false) ) //only write down if the user is not making a combination
                    if (keyValue != 164)       //if the user hold Alt, we don't want to write it until keydown is another character
                        return true;
                return false;
            }
     
            public bool WriteAltCtrl(int keyValue)
            {
                if (this.flagCtrlAlt == true && this.flagAltGr == false)
                    if (keyValue != 162 && keyValue != 163 && keyValue != 164 && keyValue != 165)   //the user is holding Ctrl + Alt, we don't want to write it until keydown is another character
                        return true;
                return false;
            }
     
            public bool WriteAltGr(int keyValue)
            {
                if (this.flagAltGr == true && this.flagCtrlAlt == false)
                    if (keyValue != 162 && keyValue != 163 && keyValue != 164 && keyValue != 165)   //the user is holding AltGr, we don't want to write it until keydown is another character
                        return true;
                return false;
            }
     
            public bool WriteF(int keyValue)
            {
                if ( this.flagF == true && (this.flagAlt == false) && (this.flagCtrlL == false) && (this.flagCtrlR == false) && (this.flagAltGr == false) && (this.flagCtrlAlt == false) )
                    if ((keyValue != 112) || (keyValue != 113) || (keyValue != 114) || (keyValue != 115) || (keyValue != 116) || (keyValue != 117) || ((keyValue != 118) || (keyValue != 119) || (keyValue != 120) || (keyValue != 121) || (keyValue != 122) || (keyValue != 123)) ) //F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12 are recorded just once, even if the user kept them pressed
                        return true;
                return false;
            }
     
            #endregion
     
     
            #region getters and setters
     
            public bool FlagCtrlL
            {
                get{ return this.flagCtrlL; }
                set { this.flagCtrlL = value; }
            }
     
            public bool FlagCtrlR
            {
                get { return this.flagCtrlR; }
                set { this.flagCtrlR = value; }
            }
     
            public bool FlagAlt
            {
                get { return this.flagAlt; }
                set { this.flagAlt = value; }
            }
     
            public bool FlagCtrlAlt
            {
                get { return this.flagCtrlAlt; }
                set { this.flagCtrlAlt = value; }
            }
     
            public bool FlagAltGr
            {
                get { return this.flagAltGr; }
                set { this.flagAltGr = value; }
            }
     
            public bool FlagF
            {
                get { return this.flagF; }
                set { this.flagF = value; }
            }
     
            #endregion
        }
    }
    Bon week end a tous,
    Vincent

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [VB.Net] isPostBack est toujours False
    Par graphicsxp dans le forum ASP.NET
    Réponses: 10
    Dernier message: 28/02/2006, 18h23
  2. hook clavier
    Par bou3 dans le forum Windows
    Réponses: 5
    Dernier message: 14/06/2005, 17h12
  3. Réponses: 11
    Dernier message: 14/01/2005, 17h29
  4. Hook Clavier et changement de touche
    Par Fly_57 dans le forum API, COM et SDKs
    Réponses: 6
    Dernier message: 09/08/2004, 19h42
  5. [Hook] Clavier Microsoft office keyboard
    Par merzhin dans le forum API, COM et SDKs
    Réponses: 2
    Dernier message: 01/06/2004, 13h59

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo