Lecture tampon clavier C

Résolu/Fermé
benorde33 - 31 mai 2010 à 19:38
 benorde33 - 31 mai 2010 à 22:35
Bonjour,

Je suis en train de développer un programme en C (console Windows) permettant d'activer des sorties et réceptionner l'état de plusieurs entrée.

Mon problème est le suivant: je souhaiterai que les entrées soit constamment actualisées et affichées sur mon écran (exécution d'un bout de programme déjà écrit) mais qu'en cas d'appui d'une touche sur le clavier (pour changer l'état d'une sortie) soit détectée.

j'ai essayé la fonction scanf et getch(), mais cela stoppe le programme et attend qu'une touche soit appuyée (l'actualisation des entrées à l'écran ne s'actualise sonc plus).

Je chercherai un moyen de pouvoir connaitre si une touche a été appuyée avant d'exécuter un getch façon:


...
if (touche clavier appuyée)
touche = getch();
...

des idées ?

Merci d'avance !!
A voir également:

3 réponses

fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 835
31 mai 2010 à 20:40
Bonjour,

Tu as la fonction _kbhit() dans conio.h.
Donc tu peux faire :
if(_kbhit()) {
     touche=_getch()
}


Cdlt,
1
Bonjour,

Il faut choper du code de l'API Windows ^^

voila du code que j'ai retrouver, copie colle et tu auras un programme fonctionnel pouvant relever les IO clavier souris, .. j'espère pas y avoir mi de superflu ou d'autres truc inutiles :'| cependant ca reste lourd, ...

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>

typedef struct
{
    BOOL keyState[256];
    int controlKeyState;
} KEYBOARD_DATA;

typedef struct
{
    int state;
    char* text;
    int textLength;
    int attributes;
    short x;
    short y;
    short cursorX;
} TextInput;

static HANDLE _hStdout = NULL;
static HANDLE _hStdin = NULL;
static HANDLE _hConsole = NULL;

static MOUSE_EVENT_RECORD _oldMouseData;
static MOUSE_EVENT_RECORD _mouseData;
static KEYBOARD_DATA _oldKeyboardData;
static KEYBOARD_DATA _keyboardData;
static char _lastKey = 0;
static TextInput _textInput;
static short _mouseWheel = 0;


void init();
void erreur(char* msg);
void ProcessEvents();
COORD Cl_GetMousePosition();
short GetWheelValue();
int IsButtonPressed(int buttonCode);
int HasButtonChanged(int buttonCode);
int HasButtonReleased(int buttonCode);
int GetLastButton();


int main()
{
	init();
	while(1)
	{
		if(HasButtonReleased(VK_SPACE))
			exit(0);
		ProcessEvents();
	}
}

void init()
{
	 _hConsole = GetConsoleWindow();
    if (_hConsole == NULL)
        erreur("");

    _hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if (_hStdout == INVALID_HANDLE_VALUE)
        erreur("");

    _hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (_hStdin == INVALID_HANDLE_VALUE)
        erreur("");

    ZeroMemory(&_textInput, sizeof(TextInput));
    _textInput.text = (char*)calloc(1, sizeof(char));
    *_textInput.text = 0;

    ZeroMemory(&_mouseData, sizeof(MOUSE_EVENT_RECORD));
    ZeroMemory(&_keyboardData, sizeof(KEYBOARD_DATA));

    srand((int)time(NULL));
}
void erreur(char* msg)
{
}
void ProcessEvents()
{
    DWORD nbEvents = 0;
    DWORD readEvents = 0;
    INPUT_RECORD* eventBuffer = NULL;
	unsigned int i;
	int j;

    _oldKeyboardData = _keyboardData;

    GetNumberOfConsoleInputEvents(_hStdin, &nbEvents);

    if (nbEvents == 0) return;

    eventBuffer = (INPUT_RECORD*)malloc(nbEvents * sizeof(INPUT_RECORD));

    ReadConsoleInputA(_hStdin, eventBuffer, nbEvents, &readEvents);

    for (i = 0; i < readEvents; i++)
    {
        if (eventBuffer[i].EventType == KEY_EVENT)
        {
            if (eventBuffer[i].Event.KeyEvent.wRepeatCount == 1)
            {
                _keyboardData.controlKeyState = eventBuffer[i].Event.KeyEvent.dwControlKeyState;
                _keyboardData.keyState[eventBuffer[i].Event.KeyEvent.wVirtualKeyCode] = eventBuffer[i].Event.KeyEvent.bKeyDown;
                _lastKey = (char)eventBuffer[i].Event.KeyEvent.wVirtualKeyCode;
            }
            if (_textInput.state == 1 && eventBuffer[i].Event.KeyEvent.bKeyDown == TRUE)
            {
                int keyCode = eventBuffer[i].Event.KeyEvent.wVirtualKeyCode;
                int charCode = eventBuffer[i].Event.KeyEvent.uChar.AsciiChar;
                switch (keyCode)
                {
                    case VK_BACK: // Backspace
                        if (_textInput.cursorX > 0)
                        {
                            _textInput.cursorX--;
                            _textInput.textLength--;
                            for (j = _textInput.cursorX; j < _textInput.textLength; j++)
                                *(_textInput.text + j) = *(_textInput.text + j + 1);
                            _textInput.text = (char*)realloc(_textInput.text, _textInput.textLength + 1);
                            *(_textInput.text + _textInput.textLength) = 0;
                        }
                        break;
                    case VK_DELETE:
                        if (_textInput.cursorX < _textInput.textLength)
                        {
                            _textInput.textLength--;
                            for (j = _textInput.cursorX; j < _textInput.textLength; j++)
                                *(_textInput.text + j) = *(_textInput.text + j + 1);
                            _textInput.text = (char*)realloc(_textInput.text, _textInput.textLength + 1);
                            *(_textInput.text + _textInput.textLength) = 0;
                        }
                        break;
                    case VK_RETURN: // Enter
                        _textInput.state = 0;
                        break;
                    case VK_LEFT:
                        if (_textInput.cursorX > 0)
                            _textInput.cursorX--;
                        break;
                    case VK_RIGHT:
                        if (_textInput.cursorX < _textInput.textLength)
                            _textInput.cursorX++;
                        break;
                    case VK_HOME:
                        _textInput.cursorX = 0;
                        break;
                    case VK_END:
                        _textInput.cursorX = _textInput.textLength;
                        break;
                    default:
                        if (charCode >= 0x20 ||
                            (keyCode >= 48 && keyCode <= 90) ||
                            (keyCode >= 219 && keyCode <= 223 && keyCode != 221) ||
                            keyCode == 186 || keyCode == 192) // Displayable chars
                        {
                            _textInput.cursorX++;
                            _textInput.textLength++;
                            _textInput.text = (char*)realloc(_textInput.text, _textInput.textLength + 1);
                            for (j = _textInput.textLength - 1; j >= _textInput.cursorX; j--)
                                *(_textInput.text + j) = *(_textInput.text + j - 1);
                            *(_textInput.text + _textInput.cursorX - 1) = charCode;
                            *(_textInput.text + _textInput.textLength) = 0;
                        }
                }
            }
        }
        else if (eventBuffer[i].EventType == MOUSE_EVENT)
        {
            if (eventBuffer[i].Event.MouseEvent.dwEventFlags < 2)
            {
                _mouseData = eventBuffer[i].Event.MouseEvent;
            }
            else if (eventBuffer[i].Event.MouseEvent.dwEventFlags == 4)
            {
                if (eventBuffer[i].Event.MouseEvent.dwButtonState & 0xFF000000)
                    _mouseWheel++;
                else
                    _mouseWheel--;
            }
        }
    }
    free(eventBuffer);
}

COORD Cl_GetMousePosition()
{
    return _mouseData.dwMousePosition;
}
short GetWheelValue()
{
	short w;
    w = _mouseWheel;
    _mouseWheel = 0;
    return w;
}
int IsButtonPressed(int buttonCode)
{
    if (buttonCode < 5)
        return (_mouseData.dwButtonState & buttonCode) == buttonCode;
    else
        return _keyboardData.keyState[buttonCode];
}
int HasButtonChanged(int buttonCode)
{
    if (buttonCode < 5)
        return ((_oldMouseData.dwButtonState & buttonCode) == buttonCode) ^ ((_mouseData.dwButtonState & buttonCode) == buttonCode);
    else
        return _oldKeyboardData.keyState[buttonCode] ^ _keyboardData.keyState[buttonCode];
}
int HasButtonReleased(int buttonCode)
{
    if (buttonCode < 5)
        return (((_oldMouseData.dwButtonState & buttonCode) == buttonCode) ^ ((_mouseData.dwButtonState & buttonCode) == buttonCode)) && !((_mouseData.dwButtonState & buttonCode) == buttonCode);
    else
        return (_oldKeyboardData.keyState[buttonCode] ^ _keyboardData.keyState[buttonCode]) && !_keyboardData.keyState[buttonCode];
}
int GetLastButton()
{
	char lk;
    lk = _lastKey;
    _lastKey = 0;
    return lk;
}




vic
0
Utilisateur anonyme
31 mai 2010 à 21:48
Désolé pour la taille ^^' et merci à Julien alias Exos (ce code fait parti d'une lib qu'il a fait)
0
Merci à vous tous pour la réponse rapide, ça fonctionne nickel !
Merci pour l'API windows que je ne connaissais pas. Ce qui pourra m'être utile pour d'autres projet (notamment la souris).
0