Keyboard Input

Introduction

Keyboard InputIn many of your programs you will need the option of accepting keyboard input. This tutorial will explain how to capture messages sent from the keyboard.

If you are using GLUT|ES, please consult the GLUT on how to make use of keyboard input with OpenGL ES. The code is almost exactly the same. Also make sure that you download the source code at the bottom of this page. The problem with the GLUT|ES library is that the entire screen is taken up by the OpenGL ES window, which hides the keyboard popup button. This will most likely result in you not using keyboard input much.

Contents of main.cpp :


The first step is to create a function that will process all keyboard input. This function must accept certain parameters.

The first parameter must accept a UGWindow variable.

The second parameter must be an integer. This specifies what key has been pressed.

The third and fourth parameters must also both be integers. These specify the x and y value of the pointing device when the key was pressed.

void keyboard(UGWindow uwin, int key, int x, int y)
{

We check to see what key has been pressed.

	switch(key)
	{

You can compare the key variable to any character as shown below. If a lower case q is pressed, the program will exit.

	case 'q' : exit(0); break;

Special keys are also available and are listed in the table below :

Identifier Description
UG_KEY_F1 - UG_KEY_F2 F1 through F12 keys.
UG_KEY_LEFT Left arrow
UG_KEY_RIGHT Right arrow
UG_KEY_UP Up arrow
UG_KEY_DOWN Down arrow
UG_KEY_PAGE_UP Page Up key
UG_KEY_PAGE_DOWN Page Down key
UG_KEY_HOME Home key
UG_KEY_END End key
UG_KEY_INSERT Insert key

Below, we exit the program if the up arrow is pressed.

	case UG_KEY_UP : exit(0); break;
	}
}

The window needs to know what function we are using for processing key presses. We therefore place a call to theugKeyboardFunc function along with the ugDisplayFunc function. This function takes the handle to the OpenGL window as its first parameter and the keyboard function as its second.

	ugKeyboardFunc(uwin, keyboard);

You should now be able to process keyboard messages. If you run this program, you will still not see any graphics occur, but you can quit the program by either pressing the q key or the up arrow.

Please let me know of any comments you may have : Contact Me

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值