c语言在vc环境下,如何实现键盘上下下左右键的触控

mengzhouzhou 2012-07-23 10:50:33
通过按键盘上的,上下左右键 来触发事件调用。 简单给提示需要什么 头文件, 比如,按一下﹤―这个键,就输出printf(“。。。”); 求一个实例。
...全文
440 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengzhouzhou 2012-07-25
  • 打赏
  • 举报
回复
非常感谢,谢谢
赵4老师 2012-07-25
  • 打赏
  • 举报
回复
Windows的Console比其它操作系统的Console功能多很多,所以使用也复杂很多。
赵4老师 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
为什么微软把控制台输出API做的这么复杂呢?

引用 8 楼 的回复:

仅供参考C/C++ code
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len)……
[/Quote]
参考
console屏幕处理例子程序。终端窗口屏幕处理相关API使用例子。来自MSVC20\SAMPLES\win32\console\
http://download.csdn.net/detail/zhao4zhong1/3461309
jokers_i 2012-07-25
  • 打赏
  • 举报
回复
为什么微软把控制台输出API做的这么复杂呢?
[Quote=引用 8 楼 的回复:]

仅供参考C/C++ code
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConso……
[/Quote]
赵4老师 2012-07-24
  • 打赏
  • 举报
回复
//The _getch function reads a single character from the console without echoing.
//Function can not be used to read CTRL+Break.
//When reading a function key or an arrow key,
//_getch must be called twice; the first call returns 0 or 0xE0,
//and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
unsigned short k;

while (1) {
Sleep(100);
k=getch();
if (27==k) break;//按Esc键退出
if (0==k||0xe0==k) k|=getch()<<8;//非字符键
cprintf("%04x pressed.\r\n",k);
}
}
mengzhouzhou 2012-07-24
  • 打赏
  • 举报
回复
int mykeyboard::read_key_assay (void) 是c言语中的吗?
就是用C语言在vc环境下实现键盘的方向控制还有高亮显示行??
mengzhouzhou 2012-07-24
  • 打赏
  • 举报
回复
只用C语言实现,该怎么办。实现方向键控制,还有高亮显示。还是在VC编译环境下???求啊!!!
夏天__ 2012-07-24
  • 打赏
  • 举报
回复
赵4老师 2012-07-24
  • 打赏
  • 举报
回复
仅供参考
#include <windows.h>
#include <stdio.h>

void ConPrint(char *CharBuffer, int len);
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);

int main(int argc, char* argv[])
{
HideTheCursor();
ClearConsoleToColors(15, 1);
ClearConsole();
gotoXY(1, 1);
SetColor(14);
printf("This is a test...\n");
Sleep(5000);
ShowTheCursor();
SetColorAndBackground(15, 12);
ConPrint("This is also a test...\n", 23);
SetColorAndBackground(1, 7);
ConPrintAt(22, 15, "This is also a test...\n", 23);
gotoXY(0, 24);
SetColorAndBackground(7, 1);
return 0;
}

//This will clear the console while setting the forground and
//background colors.
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
//not used but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}

//This will clear the console.
void ClearConsole()
{
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
}

//This will set the position of the cursor
void gotoXY(int x, int y)
{
//Initialize the coordinates
COORD coord = {x, y};
//Set the position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
}

//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}

//Direct console output
void ConPrint(char *CharBuffer, int len)
{
DWORD count;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), CharBuffer, len, &count, NULL);
}

//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
DWORD count;
COORD coord = {x, y};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hStdOut, coord);
WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}

//Hides the console cursor
void HideTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = FALSE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}

//Shows the console cursor
void ShowTheCursor()
{
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

if(GetConsoleCursorInfo(hStdOut, &cciCursor))
{
cciCursor.bVisible = TRUE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}

mengzhouzhou 2012-07-24
  • 打赏
  • 举报
回复
在vc下用C语言如何实现菜单 在方向键上下的控制下,实现按下向下键就显示就是菜单中选项向下移一位并高亮显示出来??很困惑。我是新手。不知道该用到什么函数,什么头文件。求解释代码示例。。
mengzhouzhou 2012-07-24
  • 打赏
  • 举报
回复
谢谢楼上的,非常感谢。很牛。
野狼孤 2012-07-23
  • 打赏
  • 举报
回复
其实方向键 按一下 会产生3个对应的编码 所以需要做3次选择 这里给你一段代码 都是我一直用的
//从键盘读数据,并分析其功能
int mykeyboard::read_key_assay (void)
{
//定义输入字符的保存变量
int ch = my_getch();
#if 1
//取出个功能键
if (27 == ch)
{
ch = my_getch ();

//其他的功能键
if (91 == ch)
{
ch = my_getch ();

//方向键上下页等功能键
switch (ch)
{
//方向键
case 65:
//cout << "当前的是向上键" << endl;
return MY_UP;
break;
case 66:
//cout << "当前的是向下键" << endl;
return MY_DOWN;
break;
case 67:
//cout << "当前的是向右键" << endl;
return MY_RIGHT;
break;
case 68:
//cout << "当前的是向左键" << endl;
return MY_LEFT;
break;
}
}

//统一出口
goto _out;
}
//取出字母键
if ((31 < ch) && (127 > ch))
{
// cout << "你当前按下的是字符键: " << (char)ch << endl;
return MY_INSERT;
}

#endif

_out:
return 0;
}

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧