[Help] C Programming

Status
Not open for further replies.

tuxfan

Technomancer
vasanth_12345 said:
coldn't undestand it tuxfan "int far *capstat" far is a variable right.Couldnt uderstand his properly "state = *capstat & 64" what does the state function do.Further "0x417" denotes the ram value right then the keyboards buffer rests in the ram and not in the keyboard right.

int far *capstat creats a far integer pointer variable

state is also an integer variable only and not any specific function.

0x417 is the address in the memory that stores the current status of caps, num, insert, etc. Its just one (or two) byte. Compare the functions that I have posted. They are almost same except the state = *capstat & 64 statement. One has 64, other has 32, another one has 16, etc. So one byte, different bits store these values. :)

Keyboard buffer is different. Keyboard buffer stores the keypresses till the OS can handle them. At times what happens is that an application is busy in process A and an impatient user keeps pressing some keys or the other.

That could create unexpected processing to start with the application after that long process A is over. So I also have a function called clearkeybuff() 8) :p So I can call this function immediately after process A is over. Nothing great in code. Its pretty common sense with 4-5 lines. Here it is

Code:
void clrkeybuff()
{
     int istherech = 1, dummy;
     while ( istherech != 0 )
     {
          istherech = bioskey(1);
          if ( istherech != 0 )
               dummy = bioskey(0);
     }
}

vasanth_12345 said:
pls correct me if i am wrong further all the keys can be accessed like this or what.Pls help
Which other key do you want to access like this? Is it necessary to access any one of them like this? Just think :) Caps, Num, Scroll, Shift, Ctrl, Alt, etc. are basically dormant key and doesn't send any report to the application once they are pressed. So to capture their state or to find if they are pressed, you need to write code.
 

technovice

Broken In
tuxfan said:
Which other key do you want to access like this? Is it necessary to access any one of them like this? Just think :) Caps, Num, Scroll, Shift, Ctrl, Alt, etc. are basically dormant key and doesn't send any report to the application once they are pressed. So to capture their state or to find if they are pressed, you need to write code.

Just wanted to know if the escape key can also be detected in a similar manner?

Also how is it possible to have i/p through a mouse in a c program?
 

hafees

In the zone
Just wanted to know if the escape key can also be detected in a similar manner?
ESC key is not a toggle key. that is once u pressed ESC key it will generate a scan code. think about Caps LOck, it is a toggle key. once u pressed it it becomes on, if u press again it becomes off. But how will u detect the present status of the Caps Lock. that is y their states is stored in RAM.

to detect whether ESC key is pressed, u only need to check the ascii code.
eg:
main()
{
char ch;
ch=getch();
if(ch==27) printf("Esc Key is pressed");
}
 

tuxfan

Technomancer
technovice said:
thanks hafees!
btw do you have the ascii codes for all other keys
say left, right, down, up etc

Here you go :D :D Save this file in notepad and name it as key_code.h In your code #include the file and you can start using it. Enjoy :D :D As for mouse with C programs, I have another set of .h files and another set of functions. :) More on that later.

Code:
/***********************************************************
*
*   key_code.h
*
*   Header file containing Scan Codes
*
*   (c) Copyright all C programmers, 1994.
*   All Rights Reserved.
*
*   This file released under GNU/GPL on 31st Mar '05
*   File released by tuxfan @ Digit Forums
*
************************************************************/

#define ESC        0x1b
#define ENTER      0xd
#define TAB        0x09
#define SHIFT_TAB  0x0f
#define PGUP       0x49
#define PGDN       0x51
#define UPARROW    0x48
#define DNARROW    0x50
#define LEFTARROW  0x4b
#define RIGHTARROW 0x4d
#define HOME       0x47
#define END        0x4f
#define BACKSPACE  0x08
#define INSERT     0x52
#define DELETE     0x53
#define NUM_PAD_5  0x4c   /*  with Num Lock off  */

#define CTRL_ENTER 0xa
#define CTRL_TAB   0x94
#define CTRL_PGUP  0x84
#define CTRL_PGDN  0x76
#define CTRL_UP    0x8d
#define CTRL_DN    0x91
#define CTRL_RIGHT 0x74
#define CTRL_LEFT  0x73
#define CTRL_HOME  0x77
#define CTRL_END   0x75
#define CTRL_BKSP  0x7f
#define CTRL_INS   0x92
#define CTRL_DEL   0x93

#define ALT_ENTER  0x1c
#define ALT_TAB    0xa5
#define ALT_PGUP   0x99
#define ALT_PGDN   0xa1
#define ALT_UP     0x98
#define ALT_DN     0xa0
#define ALT_LEFT   0x9b
#define ALT_RIGHT  0x9d
#define ALT_HOME   0x97
#define ALT_END    0x9f
#define ALT_BKSP   0xe
#define ALT_INS    0xa2
#define ALT_DEL    0xa3

#define F1         0x3b
#define F2         0x3c
#define F3         0x3d
#define F4         0x3e
#define F5         0x3f
#define F6         0x40
#define F7         0x41
#define F8         0x42
#define F9         0x43
#define F10        0x44
#define F11        0x85
#define F12        0x86

#define SHIFT_F1   0x54
#define SHIFT_F2   0x55
#define SHIFT_F3   0x56
#define SHIFT_F4   0x57
#define SHIFT_F5   0x58
#define SHIFT_F6   0x59
#define SHIFT_F7   0x5a
#define SHIFT_F8   0x5b
#define SHIFT_F9   0x5c
#define SHIFT_F10  0x5d
#define SHIFT_F11  0x87
#define SHIFT_F12  0x88

#define CTRL_F1    0x5e
#define CTRL_F2    0x5f
#define CTRL_F3    0x60
#define CTRL_F4    0x61
#define CTRL_F5    0x62
#define CTRL_F6    0x63
#define CTRL_F7    0x64
#define CTRL_F8    0x65
#define CTRL_F9    0x66
#define CTRL_F10   0x67
#define CTRL_F11   0x89
#define CTRL_F12   0x8a

#define ALT_F1     0x68
#define ALT_F2     0x69
#define ALT_F3     0x6a
#define ALT_F4     0x6b
#define ALT_F5     0x6c
#define ALT_F6     0x6d
#define ALT_F7     0x6e
#define ALT_F8     0x6f
#define ALT_F9     0x70
#define ALT_F10    0x71
#define ALT_F11    0x8b
#define ALT_F12    0x8c

#define ALT_A      0x1e
#define ALT_B      0x30
#define ALT_C      0x2e
#define ALT_D      0x20
#define ALT_E      0x12
#define ALT_F      0x21
#define ALT_G      0x22
#define ALT_H      0x23
#define ALT_I      0x17
#define ALT_J      0x24
#define ALT_K      0x25
#define ALT_L      0x26
#define ALT_M      0x32
#define ALT_N      0x31
#define ALT_O      0x18
#define ALT_P      0x19
#define ALT_Q      0x10
#define ALT_R      0x13
#define ALT_S      0x1f
#define ALT_T      0x14
#define ALT_U      0x16
#define ALT_V      0x2f
#define ALT_W      0x11
#define ALT_X      0x2d
#define ALT_Y      0x15
#define ALT_Z      0x2c

#define CTRL_A     0x1
#define CTRL_B     0x2
#define CTRL_C     0x3
#define CTRL_D     0x4
#define CTRL_E     0x5
#define CTRL_F     0x6
#define CTRL_G     0x7
#define CTRL_H     0x8
#define CTRL_I     0x9
#define CTRL_J     0xa
#define CTRL_K     0xb
#define CTRL_L     0xc
#define CTRL_M     0xd
#define CTRL_N     0xe
#define CTRL_O     0xf
#define CTRL_P     0x10
#define CTRL_Q     0x11
#define CTRL_R     0x12
#define CTRL_S     0x13
#define CTRL_T     0x14
#define CTRL_U     0x15
#define CTRL_V     0x16
#define CTRL_W     0x17
#define CTRL_X     0x18
#define CTRL_Y     0x19
#define CTRL_Z     0x1a

#define PLUS       0x2b
#define MINUS      0x2d

#define TRUE       1
#define FALSE      0
#define AND        &&
#define OR         ||
 

aadipa

Padawan
For checking key strocks, getch() may not work for all keys.

getch() returns ASCII code of char you pressed but some keys will only work correctly with key code.

So it is better to handle key codes than ascii codes.


Every key on key board is assigned to different key code.

Also use a separate function to get the status of special keys like LEFT/RIGHT SHIFT/CTRL/ALT keys.

You can get all required intrupts to read scan code and special key status in good old "Let Us C"
 

technovice

Broken In
HI tuxfan!!
fantafabulous man!! :)
thanks for the header files

only one more thing...
can u please explain how to use it in our functions (an eg would be great!)
 

hafees

In the zone
For checking key strocks, getch() may not work for all keys.

getch() returns ASCII code of char you pressed but some keys will only work correctly with key code.

in such cases getch() can be used twice to get the scan code. For eg: Arrow keys don't have an ascii code. in that case u can either use bioskey() or getch() twice. eg:
Code:
//this program prints the corresponding Ascii value or scan code of the key 
//that is being pressed. Press ESC to quit.

#define ESC 27
int main()
{
	char ch;
	do
	{
		ch=getch();
		if(ch==0) //that is no ascii code
		{
			ch=getch();
			printf("\nScan Code is %d",ch);
		}
		else
			printf("\nAscii Code is %d",ch);

	}
	while(ch!=ESC); //loop until ESC key is pressed
}
 

hafees

In the zone
to use mouse in ur programs u should use interrupts. mouse uses interrupt 0x33.
u can use the function int86() (software interrupt) to invoke an interrupt. This is some what advanced topics in C.
For supporting mouse in ur programs copy the following code into a file (say, mouse.c) and include it in ur programs. a demonstration of functions is included. **REMEMBER : before including it in ur programs, u need to comment the main() function.

Code:
//Mouse Functions

#include<dos.h>

#define MOUSE_LEFT   1
#define MOUSE_RIGHT  2
#define MOUSE_BOTH   3

//prototypes

/*int initmouse();
void show_mouseptr();
void mouse_window(int x1,int y1,int x2,int y2);
void hide_mouse();
void mouse_status(int *x_ptr,int * y_ptr,int * button);
void set_mousecursor_to(int x,int y);
void button_press(int button,int *status,int *presses,int *x,int *y);
void button_release(int button,int *status,int *presses,int *x,int *y);
*/

int initmouse() //use this function to initialize mouse
{
	union REGS in,out;
	in.x.ax=0;
	int86(0x33,&in,&out);
	return out.x.ax;
}

void show_mouseptr() //this will show the mouse pointer
{
	union REGS in,out;
	in.x.ax=1;
	int86(0x33,&in,&out);
}

//this will define the window (don't need to use unless u need to restrict 
//the mouse pointer to some area

void mouse_window(int x1,int y1,int x2,int y2)
{
	union REGS in,out;
	in.x.ax=0x07;
	in.x.cx=x1;
	in.x.dx=x2;
	int86(0x33,&in,&out);
	in.x.ax=0x08;
	in.x.cx=y1;
	in.x.dx=y2;
	int86(0x33,&in,&out);
}

//use this to hide mouse
//before drawing or changing screen contents use this function to hide
//mouse pointer, and then show it using show_mouseptr()

void hide_mouse()
{
	union REGS in,out;
	in.x.ax=0x2;
	int86(0x33,&in,&out);
}


//this will show the mouse cursor in (x,y) position
void set_mousecursor_to(int x,int y)
{
	union REGS in,out;
	in.x.ax=0x4;
	in.x.cx=y;
	in.x.dx=x;
	int86(0x33,&in,&out);
}

//get the current mouse status
//including button information and screen location

void mouse_status(int *x_ptr,int *y_ptr,int *button)
{
	union REGS in,out;
	in.x.ax=0x3;
	int86(0x33,&in,&out);
	*x_ptr=out.x.cx/8;
	*y_ptr=out.x.dx/8;
	*button=out.x.bx;

}



//This is a demo. To include mouse functions in ur programs comment the main()
//function, and include this file eg: #include "mouse.c"

int main()
{
	int button,presses,status,x,y,flag=0;
	initmouse();
	clrscr();
	//show mouse pointer;
         show_mouseptr();
	while(1) 
	{
		
		if(bioskey(1)) //If any key is pressed
		{
			char ch=getch();
			if(ch==27) break;  //if itis the esc key then quit
		}

		mouse_status(&x,&y,&button);
		if(button & MOUSE_LEFT) //if Left button is down
		{
			if(!flag) printf("\n Mouse down at (%d,%d)",x,y);
			flag=1;

		}
		else
		{
			if(flag)  //or if (flag==1)
			{
				flag=0;
				printf("\n Mouse Up at (%d,%d)",x,y);
			}
		}
	}
}
 

aadipa

Padawan
You can also change the mouse pointer in C graphics.

Code:
union  REGS i,o;
struct SREGS s;

// Some mouse pointer shapes

int cursor[][32]={
  /*cursor1*/
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  0x0010,0x0010,0x0010,0x0010,
  /*cursor2*/
  0xff00,0xff00,0xff00,0xff00,
  0x1234,0x1234,0x1234,0x1234,
  0x0000,0x0010,0x0010,0x0000,
  0x0000,0x0010,0x0010,0x0000,
  0x0010,0x0000,0x0000,0x0000,
  0x1234,0x0034,0x0034,0x1234,
  0x5678,0x5789,0x5789,0x5678,
  0x99ff,0x9a0f,0x9a0f,0x99ff,
  /*cursor3*/
  0x0000,0x0000,0x0000,0x0000,
  0x0000,0x0000,0x0000,0x0000,
  0x0000,0x0000,0x0000,0x0000,
  0x0000,0x0000,0x0000,0x0000,
  0xffff,0xffff,0xffff,0x0007,
  0x0007,0xeee7,0x0007,0x0007,
  0xeee7,0x0007,0x0007,0xeee7,
  0x0007,0x0007,0xeee7,0xeee7,
  /*cursor4*/
  0x3fff,0x3fff,0xdfff,0xdaff,
  0xe5ff,0xfdff,0xf07f,0xeabf,
  0xe5ff,0xedbf,0xf231,0x1aa0,
  0xf064,0xc000,0xffe0,0xffe4,
  0xc000,0xc000,0x2000,0x2400,
  0x1a00,0x0200,0x0f80,0x1540,
  0x1dc0,0x1240,0x1dce,0x155f,
  0x0f9b,0x001f,0x001f,0x001b

  };

void changecursor(int *shape)
{ i.x.ax = 9;
  i.x.bx = 0;
  i.x.cx = 0;
  i.x.dx = (unsigned)shape;
  segread(&s);
  s.es = s.ds;
  int86x(0x33,&i,&i,&s);
  return;
  }

You can call it as
Code:
changecursor(cursor[i]);   // cursor[i]


Also the function to read scan codes

Code:
/*returns scan code of the key that has been hit*/
//Up : 72 Down : 80 Left : 75 Right : 77

int getkey()
{ while(!kbhit()) delay(25);
  i.h.ah=0x00;
  int86(0x16,&i,&o);
  return(o.h.ah);
  // ah : scan code
  // al : ascii code
  }

// This will return the scan code without waiting
int xgetkey()
{ while(!kbhit()) delay(25);
  i.h.ah=0x00;
  int86(0x16,&i,&o);
  return(o.h.al);
  // ah : scan code
  // al : ascii code
  }

// Get special key status

unsigned char getShiftStatus() {
   i.h.ah=0x02;
   int86(0x16,&i,&o);
   return(o.h.al);
   // Bit No : Status
   // 0	     : Right shift depressed
   // 1      : Left shift depressed
   // 2      : Ctrl depressed
   // 3      : Alt depressed
   // 4      : Scroll lock on
   // 5      : Num lock on
   // 6      : Caps lock on
   // 7      : Insert on
   }
 

technovice

Broken In
boy oh boy!!!
these are some really bhaari codes :)
thanks for these tuxfan, aadipa, and hafees
you guys must be hardcore programmers?
great job
 

tuxfan

Technomancer
technovice: Here's your chance to boost your c functions collection. Mind you, all these functions seem to be general purpose ones so can be used and reused and resued and .... :) At lease ones that I post are general purpose and I have used them many times over in different programs :)

technovice said:
can u please explain how to use it in our functions (an eg would be great!)
I think before I could come here, examples have already flown in :) Check out hafees's post.
 

Sourabh

Laptoping
lol i even found the C in my first sem Engg difficult for starters

now its C++, i find it a lil easier dunno y
 

tuxfan

Technomancer
BTW, I must mention that a CASE statement will be more useful instead of repetitive if(). Check out the syntax of CASE.

Sourabh said:
lol i even found the C in my first sem Engg difficult for starters

now its C++, i find it a lil easier dunno y

Thats great!! As for OOPS, it is more of a thinking pattern than anything else. If you can think in objects, you can take to OOPS like fish to water. You seem to be doing that :)
 

aadipa

Padawan
No way.....

Infact u will need to unlearn some basics of C++ to learn Java. (Like, Java don't support multiple inheritance directly which is used in almost every big program in C++)

I learned C, mastered it, then moved on to learn Java, and finally C++.

Now I am working in Java. So I know all these langueges.

Make sure about C as it is the basic in terms of logic development and syntax for Java and C++.

About books on C++ and Java
*mindview.net/Books

Even some recent Digit had these e-books.
 
Status
Not open for further replies.
Top Bottom