2012-10-03 27 views
1

我知道如何使用事件來測試某個鍵是否被按下,但在C中我從來沒有找到如何做到這一點。在C中讀取按鍵。輸入密鑰

我真正想要的是一個Keylistener,可以聽上,下,左,右鍵。我需要它在Linux中工作,所以沒有Windows庫。如果可能的話,使用第三方庫是我的最佳選擇。

INT主要(){

//按鍵偵聽器{

//如果(鍵==高達){//做一些事情}

//如果(鍵==下){//做一些事情}

//如果(鍵==左){//做一些事情}

//如果(鍵==右){//做一些事情}

//}

}

+1

做你想做的)終端接收按鍵,B),在窗口中,C)中的X窗口系統的背景是什麼? –

+0

我正在使用SDL庫以可視方式顯示程序,但我只是希望能夠閱讀按鍵 – Firre

+0

好吧,只需一個普通鍵盤,無論它是什麼。在C#中,您只需使用KeyValue() – Firre

回答

5

如何使用SDL讀取鍵盤了。

SDL_Event event; 
    . 
    . 
    /* Poll for events. SDL_PollEvent() returns 0 when there are no */ 
    /* more events on the event queue, our while loop will exit when */ 
    /* that occurs.             */ 
    while(SDL_PollEvent(&event)){ 
    /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */ 
    switch(event.type){ 
     case SDL_KEYDOWN: 
     printf("Key press detected\n"); 

     if (event.key.keysym.sym==SDLK_UP) 
      printf("It was the UP key\n"); 

     break; 
     case SDL_KEYUP: 
     printf("Key release detected\n"); 
     break; 

     default: 
     break; 
    } 
    } 
    . 
    . 

來源:http://www.libsdl.org/docs/html/guideinputkeyboard.html

+0

不錯,我如何捕獲SDL_KEYDOWN中的向上鍵? – Firre

+0

咦?向上鍵是SDL_KEYUP。 – zwol

+0

@Zack你錯了,SDL_KEYUP意思是釋放 –