⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kb.c

📁 用C语言及汇编用的,用于测试键盘的程序,有兴趣地可以
💻 C
📖 第 1 页 / 共 3 页
字号:
				{
					ExtE1Key[i].Status=PRESSED;
					SetColor(WHITE,BLUE);
					gotoxy(ExtE1Key[i].x,ExtE1Key[i].y);
					cprintf("%s",ExtE1Key[i].str);
				}
			//QuitFlag=CheckPressed();
			}
		}

		if(ext_Num==2) //E1 has 2 extened code
		{
			if(leading_ext_Num++==1)  // Endin E1 sequence.0,1,2....
			{                         // E1 has 2 leading code.
				leading_ext_Num=0;    // Reset Flag;
			}
				leadingFlag=0;        //Reset leading Flag
		}
   }
   //=======================
   //Processing Stand Code
   //=======================
   else
   {
		//Draw_Frame(X1+32,Y1+18,X1+64,Y1+20,LIGHTGREEN,BLACK);
		gotoxy(X1+34,Y1+19);printf("[Scan Code:                    ");
		gotoxy(X1+46,Y1+19);printf("%x ",keyCode);
		gotoxy(X1+65,Y1+19);printf("]");
		if((keyCode & bit7) != bit7 ) //if hold down the key, bit 7=0
		{
			//CheckTiming();

			for(i=0;i<StandMax;i++)
			{
				if((StandKey[i].keyCode==keyCode) && (StandKey[i].Status!=PRESSED))
				break;
			}
			if((i != StandMax) && (StandKey[i].Status!=PRESSED))
			{
				StandKey[i].Status=PRESSED;
				SetColor(WHITE,BLUE);
				gotoxy(StandKey[i].x,StandKey[i].y);
				cprintf("%s",StandKey[i].str);
				if(StandKey[i].keyCode==0x1c)
				{
					gotoxy(X1+56,Y1+9);
					cprintf("     ");
					gotoxy(X1+56,Y1+10);
					cprintf("     ");
				}
			}
		}

   }
   //preKeyCode=keyCode;  //Save KeyCode
   TurnOff_Interrupt();
   enable();
   Send8042Cmd(Enable_KB); // ensure keyboard is enable
}
//=======================================================
// Restore the old INT 9 handler
//=======================================================
void Restore_OLD_INT9H(void)
{
	setvect(0x09,oldint9);
}
//=======================================================
// Turn off interrupt
//=======================================================
void TurnOff_Interrupt(void)
{
	outportb(Port_20,End_INT);
}
//=======================================================
//Clear 8042 output buffer.
//=======================================================
void CLR_8042OBF(void)
{
	unsigned short int Status;
	Status=inportb(Port_64);
	while(0 != (Status & bit0))   // Quit loop if no data, bit0=0;
	{
		Status=inportb(Port_60); // Read Data.
		Status=inportb(Port_64); // Get the status again.
	}
}

//=======================================================
//Wait for 8042 input buffer ready.
//=======================================================
void Wait_8042_InBuf(void)
{
	unsigned short int Status;
	Status=inportb(Port_64);
	while(0 != (Status & bit1))   // Quit loop, if no command in input buffer, bit1=0;
	{
		Status=inportb(Port_64); // Get the status again.
	}
}
//=======================================================
// Wait for 8042 data output to output buffer.
//=======================================================
void Wait_8042_OutBuf(void)
{
	unsigned short int Status;    // bit0=0 no data in buffer.
	Status=inportb(Port_64);      // bit0=1 buffer full.
	while(1 != (Status & bit0))   // Quit loop, if data already in the buffer, bit0=1;
	{
		Status=inportb(Port_64);     // Get the status again.
	}
}
//=======================================================
// Read data from 8042 output buffer.
//=======================================================
int Read_8042Data(void)
{
	unsigned short int Status;
	Wait_8042_OutBuf();
	Status=inportb(Port_60);
	return Status;
}
//=======================================================
// Write data to 8042 output buffer.
//=======================================================
void Write_8042Data(int data)
{
	Wait_8042_InBuf();     // Wait for input buffer entry
	outportb(Port_60,data);
	Wait_8042_InBuf();     // Make sure 8042 run your command.
}
//=======================================================
// Send normal command to 8042.
//=======================================================
void Send8042Cmd(int cmd)
{
	Wait_8042_InBuf();     // Wait for input buffer entry
	outportb(Port_64,cmd);  // Send Command
	Wait_8042_InBuf();     // Make sure 8042 run your command.
}
void SetColor(int front,int back)
{
	textcolor(front);
	textbackground(back);
}
void Draw_Window(void)//int x1,int y1,int x2,int y2)
{
	int i,j;

	//======================
	// Draw Pannel
	//======================
	/*SetColor(LIGHTGREEN,BLACK);
	for(i=y1;i<=y2;i++)
	{
		for(j=x1-1;j<=x2;j++)
		{
			gotoxy(j,i);cprintf("\xdb");
		}
	}
	SetColor(LIGHTGREEN,BLACK);
	for(i=y1+1;i<y2;i++)
	{
		for(j=x1;j<x2;j++)
		{
			gotoxy(j,i);cprintf("\xb1");
		}
	}*/
	//======================
	// Draw Stand Keys
	//======================
	SetColor(BLACK,LIGHTGREEN);
	for(i=0;i<StandMax;i++)
	{
		if(StandKey[i].Status == NOT_YET )
		{
			if((i<=13 && i>=1)||(i<=27 && i>=16)||(i<=41 && i>=30)||(i<=53 && i>=43)||(i == 60))
				SetColor(BLACK,WHITE);
			else
				SetColor(BLACK,LIGHTGREEN);
			gotoxy(StandKey[i].x,StandKey[i].y);
			cprintf("%s",StandKey[i].str);
		}
	}
	//======================
	// Draw Extened E0 Keys
	//======================
	SetColor(BLACK,LIGHTGREEN);
	for(i=0;i<ExtE0Max;i++)
	{
		if(ExtE0Key[i].Status == NOT_YET )
		{
			if(i == 10)
				SetColor(BLACK,YELLOW);
			else
				SetColor(BLACK,LIGHTGREEN);
			gotoxy(ExtE0Key[i].x,ExtE0Key[i].y);
			cprintf("%s",ExtE0Key[i].str);
		}
	}
	//======================
	// Draw Extened E1 Keys
	//======================
	SetColor(BLACK,LIGHTGREEN);
	for(i=0;i<ExtE1Max;i++)
	{
		if(ExtE1Key[i].Status == NOT_YET )
		{
			gotoxy(ExtE1Key[i].x,ExtE1Key[i].y);
			cprintf("%s",ExtE1Key[i].str);
		}
	}
	// For ENTER key
	gotoxy(X1+56,Y1+9);
	cprintf("     ");
	gotoxy(X1+56,Y1+10);
	cprintf("     ");
}
int CheckPressed(void)
{
	unsigned short int retFlag=1,i;
	//======================
	// Check Stand Keys
	//======================
	for(i=0;i<StandMax;i++)
	{
		if(StandKey[i].Status == NOT_YET || StandKey[i].Status == MID)
		{
			retFlag=0;
		}
	}
	//======================
	// Check Extened E0 Keys
	//======================
	for(i=0;i<ExtE0Max;i++)
	{
		if(ExtE0Key[i].Status == NOT_YET || ExtE0Key[i].Status == MID)
		{
			retFlag=0;
		}
	}
  //======================
  // Check Extened E1 Keys
  //======================
	for(i=0;i<ExtE1Max;i++)
	{
		if(ExtE1Key[i].Status == NOT_YET || ExtE1Key[i].Status == MID)
		{
			retFlag=0;
		}
	}
	return retFlag;
}
void Draw_Frame(int left,int top,int right,int down,int front,int back)
{
	int i;
	union REGS myReg;
	myReg.x.ax=0x0600;   //640*200
	myReg.h.cl=left-1;
	myReg.h.ch=top-1;
	myReg.h.dl=right-1;
	myReg.h.dh=down-2;
	myReg.h.bh=front;
	int86(0x10,&myReg,&myReg);   //Cleanup text
	textcolor(front);
	textbackground(back);
	//clrscr();
	window(1,1,80,25);
	gotoxy(left,top);
	cprintf("\xc9");
	for(i=left+1;i<right;i++)
	cprintf("\xcd");
	cprintf("\xbb");
	gotoxy(left,down);
	cprintf("\xc8");
	for(i=left+1;i< right;i++)
	cprintf("\xcd");
	cprintf("\xbc");
	gotoxy(left,top+1);
	for(i=top+1;i<down;)
	{
		cprintf("\xba");
		gotoxy(left,++i);
	}
	gotoxy(right,top+1);
	for(i=top+1;i<down;)
	{
		cprintf("\xba");
		gotoxy(right,++i);
	}

}
void CheckTiming(void)
{
	if(PressKeyFlag==0)
	{
		PressKeyFlag=1;
		t_Start=clock();
	}
	else
	{
		PressKeyFlag=0;
		t_End=clock();
	}
	if(PressKeyFlag==0)
	{
		var=  ((t_End-t_Start)/CLK_TCK)  ;
		Draw_Frame(X1+2,Y1+17,X1+31,Y1+19,LIGHTGREEN,BLACK);//Clean up window
		gotoxy(X1+4,Y1+18);printf("Timing : %2.3f",var);
		if( (var *1000) <= 50 ) // Timing < 500 ms
		printf("\x7");
	}
}

char *Read_BIOSVer(int segAddr,int offAddr)
{
	void far *address;
	unsigned bufseg,bufoff;
	address=(void far *)buffer;
	bufseg=FP_SEG(address);
	bufoff=FP_OFF(address);
	movedata(segAddr,offAddr,bufseg,bufoff,16);
	*(buffer+17)=0x00;
	return buffer;
}
void help(int cmd)
{

	if(cmd==0)
	{
	printf("[KB.EXE]      REV: 1.00\n");
	printf("Copyright(C)  Quanta Computer Corporation  2005\n");
	printf("Usage:\n");
	printf("  -1. All Key    -2. Special\n");
	printf("  -3. Get Code   -4. KBC Reset\n");
	printf("  -5. Disable KB -6. Enable KB\n");
	printf("  -7. Special2    0. Quit\n");
	printf("  -");
	}
	else if(cmd==1)
	{
		printf("  -21. INTEL8259  -64. KBC8042\n");
		printf("  -9. Ret\n");
		printf("  -");
	}



}
//get scan code function
int getcode(void)
{
	clrscr();
	QuitFlag=0;
	RetestFlag=0;
	gotoxy(X1+4,2);printf("Copyright(C) Quanta Computer Inc. 2005                 [ESC]x5  Quit");
	gotoxy(X1+4,3);printf("Utility to get scan code");
	//Draw_Frame(X1+18,Y1+7,X1+50,Y1+9,WHITE,BLACK);
	gotoxy(X1+27,Y1+8);
	printf("[");
	gotoxy(X1+37,Y1+8);
	printf(":");
	gotoxy(X1+57,Y1+8);
	printf("]");
	oldint9 = getvect(9);      /* store old interrupt vector */
	setvect(0x09,Code_Int9H);   /* set up new interrupt handler */
	while(QuitFlag==0)
	{
		/*if(RetestFlag==1)
		{
			RetestFlag=0;
			RETEST=0;
			//Main_Window();
		}*/
	}
	if(FailFlag==1)
	   theResult = 1;  // Fail
	CLR_8042OBF();
	Restore_OLD_INT9H();    /* set up exit handler to restore INT 9 */
	SetColor(WHITE,BLACK);
	//delay(1);
	//CLR_8042OBF();
	clrscr();
	return theResult;    //0=Pass , 1=Fail

}
//use 8042 command to reset
void KBCreset(void)
{
	printf("<<KBC Reset>>\n");
	printf(">>Press any key");
	while(kbhit()==0)
	{
	}
	Send8042Cmd(0xfe);
}
//get kb scan code
unsigned char ScanCode(void)
{
	asm{
	   mov ah,00h
	   int 16h
	   mov al,ah
	   xor ah,ah
	   jmp done
	   }
	empty:
	   asm xor ax,ax
	done:

}
//disable kb function
void KBOFF(int cmd)
{
	switch(cmd)
	{
		case 21: { asm{ in al,21h
						or al,02h
						out 21h,al}
				   CMdelay(0x1e);
				   asm{ mov ax,4C00h
						int 21h
					  }
				   break;
				   }
		case 64: { Send8042Cmd(Disable_KB);break;}
		default: break;
	}

}
//enable kb function
void KBON(int cmd)
{
	switch(cmd)
	{
		case 21: { asm{ in al,21h
						and al,0FDh
						out 21h,al}
				   CMdelay(0x1e);
				   asm{ mov ax,4C00h
						int 21h
						}
				   break;
				   }
		case 64: {Send8042Cmd(Enable_KB);break;}
		default: break;
	}
}
void CMdelay(unsigned long delaytime)
{
	union REGS inregs;
	inregs.h.ah = 0x86;
	inregs.x.cx = (unsigned int)((delaytime & 0xFFFF0000) >> 16);
	inregs.x.dx = (unsigned int)(delaytime & 0x0000FFFF);
	int86(0x15,&inregs,&inregs);
}
//for special test, jump to follow test key
void jumpkey(int key,int stu)
{
	SetColor(BLACK,RED);
	if(stu==0)
	{
		gotoxy(StandKey[key].x,StandKey[key].y);
		cprintf("%s",StandKey[key].str);
		curkey=key;
		keystu=0;
		s++;
	}
	else if(stu==1)
	{
		gotoxy(ExtE0Key[key].x,ExtE0Key[key].y);
		cprintf("%s",ExtE0Key[key].str);
		curkey=key;
		keystu=1;
		s++;
	}


}
//error message
int emessage(int code,char *error)
{
	int ex,ey;
	int i,j,len;
	//printf("\n");
	ex=getx();
	ey=gety();
	textbackground(WHITE);
	window(ex,ey+1,ex+70,ey+4);
	SetColor(BLACK,WHITE);
	//textattr(0x38);
	highvideo();
	gotoxy(ex,ey+1);
	cprintf("[KB.EXE] ================>ERROR!                                                ");
	gotoxy(ex,ey+2);
	cprintf("%s",error);
	len=strlen(error);
	for(i=0;i<(80-len);i++)
		cprintf(" ");
	gotoxy(ex,ey+3);
	cprintf("Error Code : %d",code);
	if((code/10)==0)
		len=1;
	else
		len=2;
	for(i=0;i<(80-len-13);i++)
		cprintf(" ");
	gotoxy(ex,ey+4);
	cprintf("                                                                                ");
	//gotoxy(1,ey+4);
	//cprintf("\n");
	highvideo();
	while(kbhit()==0)
	{
		unsigned char sc;
		sc=ScanCode();
		while(sc==0x1)
			return 0;
	}
	return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -