Arnaud Aliès
Arnaud Aliès

Reputation: 1092

SDL C mouse/keyboard output

is there any ways to press keys mouse/keyboard with sdl in C?

if yes, how?

if no, do you know any ways to do that in C?

Upvotes: 1

Views: 2263

Answers (3)

programmerjake
programmerjake

Reputation: 1824

As of SDL 2.0.3, SDL doesn't support sending input events to other applications. On Windows you can use the SendInput function to send input events to other applications. With X11 you can use the xcb_send_event function. I'm not sure about OS X, iOS, or Android.

Upvotes: 2

AndrewN
AndrewN

Reputation: 711

Create an SDL_event structure and fill in the fields as documented in http://wiki.libsdl.org/SDL_KeyboardEvent and http://wiki.libsdl.org/SDL_Keysym then use SDL_Pushevent() to put the event into the event queue: http://wiki.libsdl.org/SDL_PushEvent

 SDL_Event event;

 event.type = SDL_KEYDOWN;
 event.timestamp = lastEvent.timestamp + 1;
 event.windowID - lastEvent.windowID;
 event.state = SDL_PRESSED;

 event.keysym.scancode = SDL_SCANCODE_ESCAPE; // from SDL_Keysym
 event.keysym.sym = SDLK_ESCAPE;
 event.keysym.mod = 0; // from SDL_Keymod

 SDL_PushEvent(&Event) // Inject key press of the Escape Key

Do the same thing for any other event in the SDL_Event union including mouse events: http://wiki.libsdl.org/SDL_MouseButtonEvent

Upvotes: 3

while1fork
while1fork

Reputation: 374

This program demonstrates how to read mouse and keyboard input in an SDL program. It will run for a few seconds, and display "up arrow" or "down arrow" when either of those keys are pressed, and display "mouse clicked" and coordinates when the mouse is clicked.

#include "SDL2/SDL.h"
#include <stdlib.h>

int main(){
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow( "Keyboard and mouse input",
                         SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
                         500, 500, SDL_WINDOW_SHOWN );
    int i;
    for (i = 0; i< 3000; i++){
        SDL_UpdateWindowSurface(window);
        SDL_Event event;
        while (SDL_PollEvent(&event)){//this is where the important stuff happens:
            if( event.type == SDL_KEYDOWN ) {
                switch( event.key.keysym.sym ) { 
                    case SDLK_UP: 
                        puts("up arrow");
                        break;
                    case SDLK_DOWN://for full list of key names, http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlkey.html
                        puts("down arrow");
                        break;
                }
            }
            else if (event.type == SDL_MOUSEBUTTONDOWN){
                int x, y;

                SDL_GetMouseState(&x,&y);
                printf("%s button mouse clicked at: (%d,%d)\n",
                    (event.button.button == SDL_BUTTON_LEFT)? "left" : "right",
                    x,y);
            }
        }
        SDL_Delay(1);
    }
    SDL_DestroyWindow(window);
    SDL_Quit();
}

this is for SDL version 2

basically, you need to call SDL_PollEvent, and then check the resulting SDL_Event struct for information about whether it was a keyboard or mouse event, and if so where the mouse was or what button was pressed.

Upvotes: -1

Related Questions