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

C++ Discussion :

Utiliser une variable pour un VirtualKey dans une API


Sujet :

C++

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2018
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Aube (Champagne Ardenne)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2018
    Messages : 2
    Points : 1
    Points
    1
    Par défaut Utiliser une variable pour un VirtualKey dans une API
    Bonjour,

    Depuis mon thread principal, je créé un thread secondaire où "une macro" s’exécute en boucle.

    Thread Principal :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    std::stringstream  toucheecoutee("0x51");
    std::stringstream  toucheenvoye("0x41");
    std::thread t1(IfAPressSendB, toucheecoutee, toucheenvoye);
    t1.join();

    Thread Secondaire :
    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
     
    void IfAPressSendB(stringstream KeyListened, stringstream  KeySended)
    {
        unsigned int ListenedHexVK;
        KeyListened << std::hex << "fffefffe";
        KeyListened >> ListenedHexVK;
     
        unsigned int SendedHexVK;
        KeySended << std::hex << "fffefffe";
        KeySended >> SendedHexVK;
     
        while(1)
        {
        if (GetAsyncKeyState(ListenedHexVK))
        {
            INPUT ip;
     
            ip.type = INPUT_KEYBOARD;
            ip.ki.wScan = 0;
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;
     
            ip.ki.wVk = SendedHexVK; // Hex code for 'A'
            ip.ki.dwFlags = 0; //Press the key down ?
            SendInput(1, &ip, sizeof(INPUT)); //Use function
     
            Sleep(50); //Sleep so it doesn't spam the key press
     
            ip.ki.dwFlags = KEYEVENTF_KEYUP; //Release the key
            SendInput(1, &ip, sizeof(INPUT)); //Use function
        }
        }
    }
    Au début, je convertis mes variables string (non utilisable en string puisque l'API attend un "int") en unsigned int, puis je les utilise en tant que tel. Voici les messages d'erreurs du compilateur :

    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
     
    1>------ Début de la génération : Projet : ProjetMacroR6S, Configuration : Debug Win32 ------
    1>ProjetMacroR6S.cpp
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\memory(2539): error C2664: 'std::tuple<void (__cdecl *)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>,std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>>::tuple(std::tuple<void (__cdecl *)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>,std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>> &&)' : impossible de convertir l'argument 1 de '_Ty' en 'std::allocator_arg_t'
    1>        with
    1>        [
    1>            _Ty=void (__cdecl &)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void))
    1>        ]
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\memory(2538): note: Aucun constructeur n'a pu prendre le type de source, ou la résolution de la surcharge du constructeur était ambiguë
    1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\thread(46): note: voir la référence à l'instanciation de la fonction modèle 'std::unique_ptr<std::tuple<void (__cdecl *)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>,std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>>,std::default_delete<_Ty>> std::make_unique<std::tuple<void (__cdecl *)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>,std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>>,void(__cdecl &)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::stringstream&,std::stringstream&,0>(void (__cdecl &)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::stringstream &,std::stringstream &)' en cours de compilation
    1>        with
    1>        [
    1>            _Ty=std::tuple<void (__cdecl *)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>,std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>>
    1>        ]
    1>c:\users\dridri\desktop\c++vsr6s\projet macro r6s\projetmacror6s\projetmacror6s.cpp(53): note: voir la référence à l'instanciation de la fonction modèle 'std::thread::thread<void(__cdecl &)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void)),std::stringstream&,std::stringstream&,void>(_Fn,std::stringstream &,std::stringstream &)' en cours de compilation
    1>        with
    1>        [
    1>            _Fn=void (__cdecl &)(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void))
    1>        ]
    1>Génération du projet "ProjetMacroR6S.vcxproj" terminée -- ÉCHEC.
    ========== Génération : 0 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========
    J'ai l'impression que le problème vient du multithread ! PS : Je tiens à préciser que si je ne passe pas les VK comme attribut cela fonctionne ! Donc dans le main :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    std::thread t1(IfAPressSendB);
    et le thread secondaire :
    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
     
    void IfAPressSendB(stringstream KeyListened, stringstream  KeySended)
    {
        while(1)
        {
        if (GetAsyncKeyState(0x51))
        {
            INPUT ip;
     
            ip.type = INPUT_KEYBOARD;
            ip.ki.wScan = 0;
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;
     
            ip.ki.wVk = 0x41; // Hex code for 'A'
            ip.ki.dwFlags = 0; //Press the key down ?
            SendInput(1, &ip, sizeof(INPUT)); //Use function
     
            Sleep(50); //Sleep so it doesn't spam the key press
     
            ip.ki.dwFlags = KEYEVENTF_KEYUP; //Release the key
            SendInput(1, &ip, sizeof(INPUT)); //Use function
        }
        }
    }
    Là, ça fonctionne parfaitement. Mais mon but c'est de créer une fonction réutilisable alors... Quelqu'un a une idée ? Merci

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 115
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 115
    Points : 32 967
    Points
    32 967
    Billets dans le blog
    4
    Par défaut
    std::thread s'utilise le plus simplement avec une lambda std::thread t([=](){ IfAPressSendB(toucheecoutee, toucheenvoye); });
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2018
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Aube (Champagne Ardenne)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Septembre 2018
    Messages : 2
    Points : 1
    Points
    1
    Par défaut
    Merci pour la réponse,

    Modification effectuée. Ca ne fonctionne toujours pas mais les erreurs ont changé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    1>------ Début de la génération : Projet : ProjetMacroR6S, Configuration : Debug Win32 ------
    1>ProjetMacroR6S.cpp
    1>c:\users\dridri\desktop\c++vsr6s\projet macro r6s\projetmacror6s\projetmacror6s.cpp(53): error C2664: 'void IfAPressSendB(std::stringstream (__cdecl *)(void),std::stringstream (__cdecl *)(void))'*: impossible de convertir l'argument 1 de 'const std::stringstream' en 'std::stringstream (__cdecl *)(void)'
    1>c:\users\dridri\desktop\c++vsr6s\projet macro r6s\projetmacror6s\projetmacror6s.cpp(53): note: Aucun opérateur de conversion définie par l'utilisateur disponible qui puisse effectuer cette conversion, ou l'opérateur ne peut pas être appelé
    1>Génération du projet "ProjetMacroR6S.vcxproj" terminée -- ÉCHEC.
    ========== Génération : 0 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========
    Pourquoi est ce que le compilateur veut me le convertir ?
    La ligne 53 étant : std::thread t([=]() { IfAPressSendB(toucheecoutee, toucheenvoye); });

Discussions similaires

  1. Réponses: 2
    Dernier message: 17/04/2015, 11h28
  2. [Toutes versions] Recherche de données dans une feuille pour les copier dans une autre
    Par mattdogg97 dans le forum Macros et VBA Excel
    Réponses: 6
    Dernier message: 07/02/2011, 14h22
  3. Réponses: 3
    Dernier message: 13/12/2009, 17h04
  4. [MySQL] récupérer dans une boucle chaque information MySQL dans une variable différente
    Par gtenthorey dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 06/05/2007, 22h34
  5. Réponses: 1
    Dernier message: 17/01/2007, 21h52

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