probleme SendInput() clavier virtuel
Bonjour a tous,
(dsl pour les accents j'ai un clavier QWERTY !!)
Je dois realiser un clavier virtuel en C++ sous Visual Studio 2005 (et on m’a impose MFC lol ) et je suis debutant en prog Windows et en MFC.
J’envois via la methode SendInput() le code de la touche en exadecimal a une autre fenetre ou application. J’ai bien les focus et le clavier semble marcher. Seulement il faut que j’appuis plusieurs fois sur les touches pour que la lettre s’affiche, comme si tous les messages n’etaient pas transmis ou traites.
Autre chose, j’ai une touche shift qui passe le texte des boutons en majuscule puis en minuscule apres l’envoi du message correspondant a la touche. L’appel de cette methode qui modifie l’affichage de la fenetre avant l’appel de SendInput() regle le probleme et a chaque pression d’un touche en majuscule la lettre correspondante et directement affichee dans le champ a remplir.
Je pense donc que j’ai un probleme de notification entre le thread du clavier et le processus destination.
Voila le code de la fonction qui envoi les interruptions clavier :
Code:
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
|
INPUT key[2];
::ZeroMemory(key, sizeof(key));
key[0].type =key[1].type = INPUT_KEYBOARD;
if ((vk > 65) && (vk < 90) && !shift)
{
key[1].ki.wVk = vk;
key[1].ki.wScan = vk;
key[1].ki.dwFlags = KEYEVENTF_UNICODE ;
key[0].ki.time = key[1].ki.time = 0;
key[0].ki.dwExtraInfo = key[1].ki.dwExtraInfo=0;
SendInput(2,key,sizeof(INPUT));
}
else
{
//AfxMessageBox(_T("Shift key pressed"));
shift = false;
key[1].ki.wVk = 0 ;
key[1].ki.wScan = vk ;
key[1].ki.dwFlags = KEYEVENTF_UNICODE ;
key[0].ki.time = key[1].ki.time = 0;
key[0].ki.dwExtraInfo = key[1].ki.dwExtraInfo=0;
ChangeToLowerCase();
SendInput(2,key,sizeof(INPUT));
} |
Celui de la methode PreTranslateMessage :
Code:
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
|
BOOL COSKDlg::PreTranslateMessage(MSG* pMsg)
{
HWND wnd = ::GetForegroundWindow();
if(IsWindow(wnd))
{
if(wnd != this->m_hWnd)
{
if(gFocus != wnd)
{
if(IsWindow(gFocus))
{
AttachThreadInput(
GetWindowThreadProcessId(m_hWnd,NULL),
GetWindowThreadProcessId(gFocus,NULL),
FALSE);
}
gFocus = wnd;
AttachThreadInput(
GetWindowThreadProcessId(m_hWnd,NULL),
GetWindowThreadProcessId(gFocus,NULL),
TRUE);
}
}
}
return CDialog::PreTranslateMessage(pMsg);
} |
Est celui de la fonction qui modifie le texte des buttons apres l’envois d’une touche en majuscule :
Code:
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
|
void COSKDlg::ChangeToLowerCase()
{
CString str;
GetDlgItem(IDC_BT_a)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a)->SetWindowText(str);
GetDlgItem(IDC_BT_a10)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a10)->SetWindowText(str);
GetDlgItem(IDC_BT_a11)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a11)->SetWindowText(str);
GetDlgItem(IDC_BT_a12)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a12)->SetWindowText(str);
GetDlgItem(IDC_BT_a13)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a13)->SetWindowText(str);
GetDlgItem(IDC_BT_a14)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a14)->SetWindowText(str);
GetDlgItem(IDC_BT_a15)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a15)->SetWindowText(str);
GetDlgItem(IDC_BT_a16)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a16)->SetWindowText(str);
GetDlgItem(IDC_BT_a17)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a17)->SetWindowText(str);
GetDlgItem(IDC_BT_a18)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a18)->SetWindowText(str);
GetDlgItem(IDC_BT_a19)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a19)->SetWindowText(str);
GetDlgItem(IDC_BT_a2)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a2)->SetWindowText(str);
GetDlgItem(IDC_BT_a20)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a20)->SetWindowText(str);
GetDlgItem(IDC_BT_a21)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a21)->SetWindowText(str);
GetDlgItem(IDC_BT_a22)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a22)->SetWindowText(str);
GetDlgItem(IDC_BT_a23)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a23)->SetWindowText(str);
GetDlgItem(IDC_BT_a24)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a24)->SetWindowText(str);
GetDlgItem(IDC_BT_a25)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a25)->SetWindowText(str);
GetDlgItem(IDC_BT_a26)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a26)->SetWindowText(str);
GetDlgItem(IDC_BT_a3)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a3)->SetWindowText(str);
GetDlgItem(IDC_BT_a4)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a4)->SetWindowText(str);
GetDlgItem(IDC_BT_a5)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a5)->SetWindowText(str);
GetDlgItem(IDC_BT_a6)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a6)->SetWindowText(str);
GetDlgItem(IDC_BT_a7)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a7)->SetWindowText(str);
GetDlgItem(IDC_BT_a8)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a8)->SetWindowText(str);
GetDlgItem(IDC_BT_a9)->GetWindowText(str);
str.MakeLower();
GetDlgItem(IDC_BT_a9)->SetWindowText(str);
} |
Merci d'avance.