69,381
社区成员
发帖
与我相关
我的任务
分享
TC程序如下:
#include <stdio.h>
#define esc 27
main()
{
int key,i=1;
while(1)
{
printf("+");
sleep(1);
if (i++>=5)
{i=1;
system("cls");}
while(kbhit())
{
key=getch();
if (key==esc)
{exit (0);}
printf("%c ",key);
}
}
}
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#define esc 27
int main()
{
int key, i = 0;
system("stty -echo");
while (1)
{
printf("+");
sleep(1);
if (i++ >= 5)
{
i = 0;
printf("%c[2J", 27);
gotoxy(10, 5);
}
while (kbhit())
{
key = getch();
if (key == esc)
{
if (!kbhit() || getch() != '[')
exit(0);
}
printf("%c ", key);
}
}
}
int gotoxy(x, y)
{ printf("%c[%d;%dH",27,x,y);
}
int kbhit(void)
{
struct termios oldt, newt;
int ch, oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if (ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int getch(void)
{
return getchar();
}
一般人不知道的东西:来自 城市猎人
实为printf 函数的拓展应用
可设置字背景颜色;字体颜色m字符串
int ESC =27;
ptintf("%c[参数",ESC);
//cusor_ctrl.h #ifndef __CUSOR_CTRL_H_ #define __CUSOR_CTRL_H_ #include #include #define COLOR_RED 31 #define COLOR_BLACK 30 #define COLOR_GREEN 32 #define COLOR_BLUE 34 #define COLOR_YELLOW 33 #define COLOR_WHITE 37 #define COLOR_CYAN 36 #define COLOR_MAGENTA 35 int ESC = 27; //清屏 void clear_screen(void); //清除从光标位置到行末的内容 void clear_to_end(void); //光标移动到(x,y) void cusor_moveto(int x, int y); //保存光标位置 void cusor_get_pos(void); //恢复光标位置 void cusor_set_pos(void); //光标上移num行 void cusor_up(int num); //光标下移num行 void cusor_down(int num); //光标左移num个字符 void cusor_lift(int num); //光标右移num个字符 void cusor_right(int num); //设置前景颜色 void set_fg_color(int color); //设置背景颜色 void set_bg_color(int color); #endif //__CUSOR_CTRL_H_ int main() { int x=0;int y = 0; int color=31; clear_screen(); cusor_moveto(20,10); while(1) { cusor_get_pos(); printf("(20,10)"); fflush(stdout); sleep(1); cusor_up(5); sleep(1); cusor_down(5); sleep(1); cusor_right(5); sleep(1); cusor_lift(5); sleep(1); cusor_moveto(50,10); printf("(50,10)\n"); sleep(1); set_bg_color(COLOR_BLACK); set_fg_color(COLOR_RED); printf("\ngood morning\n"); sleep(1); set_bg_color(COLOR_YELLOW); set_fg_color(COLOR_GREEN); printf("gaorenwei\n"); cusor_set_pos(); } return 0; } void clear_screen(void) { printf("%c[2J",ESC); fflush(stdout); } void clear_to_end(void) { printf("%c[K",ESC); fflush(stdout); } void cusor_moveto(int x, int y) {// ESC[y;xH printf("%c[%d;%dH",ESC,y,x); fflush(stdout); } void cusor_get_pos(void) {// ESC[s printf("%c[s",ESC); fflush(stdout); } //恢复光标位置 void cusor_set_pos(void) {// ESC[u printf("%c[u",ESC); fflush(stdout); } //光标上移num行 void cusor_up(int num) { while(num--) { // up = ESC[A printf("%c[A",ESC); } fflush(stdout); } //光标下移num行 void cusor_down(int num) { while(num--) {// down = ESC[B printf("%c[B",ESC); } fflush(stdout);} //光标左移num个字符 void cusor_lift(int num) { while(num--) {// lift = ESC[D printf("%c[D",ESC); } fflush(stdout); } //光标右移num个字符 void cusor_right(int num) { while(num--) {// right = ESC[C printf("%c[C",ESC); } fflush(stdout); } //设置前景颜色 void set_fg_color(int color) {// ESC[#m printf("%c[%dm",ESC,color); fflush(stdout); } //设置背景颜色 void set_bg_color(int color) {// ESC[#m printf("%c[%dm",ESC,(color+10)); fflush(stdout); }
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#define esc 27
int main()
{
int key,i=0;
system("stty -echo");
while(1)
{
printf("+");
sleep(1);
if (i++>=5)
{i=0;
printf("%c[2J",27);
gotoxy(10,5);
}
while(kbhit())
{
fflush(stdin);
key=getch();
if(key==esc)
{
if(!kbhit()||getch()!='[')exit(0);
}
printf("%c ",key); }
}
}
int gotoxy(x,y)
{printf("%c[%d;%dH",27,x,y);}
int kbhit(void)
{ struct termios oldt, newt;
int ch,oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{ ungetc(ch, stdin); return 1; }
return 0; }
int getch(void)
{
struct termios oldattr,newattr;
int ch;
tcgetattr(STDIN_FILENO,&oldattr);
newattr=oldattr;
newattr.c_lflag &=~(ICANON|ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&newattr);
ch=getchar();
tcsetattr(STDIN_FILENO,TCSANOW,&oldattr);
return ch;
}
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#define esc 27
int main()
{
int key,i=1;
struct termios attr;
tcgetattr(STDIN_FILENO,&attr);
attr.c_lflag &=~(ICANON|ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&attr);
while(1)
{
printf("+");
sleep(1);
if (i++>=5)
{i=1;
system("clear");}
while(kbhit())
{
fflush(stdin);
key=getch();
if(key==esc)
{
if(!kbhit()||getch()!='[')exit(0);
}
printf("%c ",key); }
}
}
int kbhit(void)
{ struct termios oldt, newt;
int ch,oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{ ungetc(ch, stdin); return 1; }
return 0; }
int getch(void)
{
struct termios oldattr,newattr;
int ch;
tcgetattr(STDIN_FILENO,&oldattr);
newattr=oldattr;
newattr.c_lflag &=~(ICANON|ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&newattr);
ch=getchar();
tcsetattr(STDIN_FILENO,TCSANOW,&oldattr);
return ch;
}
/*
AUTHOR: zobayer
INSTRUCTION:
just make this file a header like "conio.h"
and use the getch() and getche() functions.
*/
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
/* reads from keypress, doesn't echo */
int getch(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
/* reads from keypress, echoes */
int getche(void)
{
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return ch;
}
/**
* file encode: UTF-8
* date: 12-03-2010
* author: Vincent
* msn:
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
/**********************************
* defination *
**********************************/
#define ESC 27
#define ENTER 10
#define BACKSPACE 127
static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard();
void close_keyboard();
int kbhit();
int readch();
/*******************************
* implementation *
*******************************/
int main(int argc, char* argv[])
{
int ch;
ch = 0;
init_keyboard();
while(ESC != ch)
{
if(kbhit())
{
ch = readch();
switch(ch)
{
case ESC:
break;
}
}
}
close_keyboard();
return 0;
}
void init_keyboard()
{
tcgetattr(0, &initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
return;
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
return;
}
int kbhit()
{
char ch;
int nread;
if(-1 != peek_character)
{
return 1;
}
new_settings.c_cc[VMIN] = 0;
tcsetattr(0, TCSANOW, &new_settings);
nread = read(0, &ch, 1);
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
if(1 == nread)
{
peek_character = ch;
return 1;
}
return 0;
}
int readch()
{
char ch;
if(-1 != peek_character)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0, &ch, 1);
return ch;
}