[PDcurses] Handle pressed button

This is a discussion on [PDcurses] Handle pressed button within the Windows Programming forums, part of the Platform Specific Boards category; Hello. Here is a little code that i wrote with using Pdcurses library. Code: int main() { WINDOW *pad; WINDOW ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    [PDcurses] Handle pressed button

    Hello.
    Here is a little code that i wrote with using Pdcurses library.
    Code:
    int main()
    {
    
    
    	WINDOW *pad;
    	WINDOW *input_window;	
    	
    	int	height = 1;
    	int	width = 77;
    	int	starty = 26;	
    	int	startx = 0;
    
    	int line = 0;
    
    	initscr();
    	cbreak();
    
    	// Pad (pad) Properties
    	pad = newpad(100,80);
    	scrollok(pad,TRUE);
    	prefresh( pad, 0, 0, 0, 0, LINES-1, COLS-1);
    
    	// Input window (input_window) properties
    	input_window = newwin(height,width,starty,startx);
    	touchwin(input_window);
    	keypad(input_window,TRUE);
    	start_color();
    	init_pair(1,COLOR_WHITE, COLOR_RED);
    	wbkgd(input_window,COLOR_PAIR(1));
    	wmove(input_window,1,1); // Moves the cursor into the "input_window"
    	wrefresh(input_window);
    
    
    	while(TRUE)
    	{
    		if(wgetstr(input_window,chars) != ERR)
    		{
    			mvwprintw(pad,line,0,"%s\n",chars);
    			line++;
    			prefresh( pad, z, 0, 0, 0, LINES-1,COLS-1);
    			werase(input_window);
    			wmove(input_window,1,1);
    			memset(chars,0,sizeof(chars));
    			wrefresh(input_window);
    		}
    	}
    
    endwin();
    return 0;
    }
    As you can understand this code creates a window and a pad. Whatever user writes and then press Enter button appears into pad.

    Now i want to make this code to understand when user press the Page Up button e.g and print out in the pad "Page Up key pressed" without the user to has to press enter button .

    Thought to use GetAsyncKeyState function but i want to hear your ideas.

    Any help is welcomed.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    I tried this and it seems that works but it needs some changes i believe.

    Code:
    int main()
    {
    
    	HANDLE         stdinInput = 0;
    	DWORD          numEvents = 0;
    	DWORD          numEventsRead = 0;
    	DWORD		   numReceivedRecords = 0;
    	DWORD		   fdwSaveOldMode;
    	DWORD          fdwMode;
    
    	WINDOW *pad;
    	WINDOW *input_window;	
    
    	char   dataBuffer[100];
    	int    bufferLen = 0;
    	
    	int	height = 1;
    	int	width = 77;
    	int	starty = 26;	
    	int	startx = 0;
    
    	int line = 0;
    
    	stdinInput = GetStdHandle(STD_INPUT_HANDLE);
    
    	if (stdinInput == INVALID_HANDLE_VALUE) 
    			perror("GetStdHandle");
    
    	if (! GetConsoleMode(stdinInput, &fdwSaveOldMode) ) 
    			perror("GetConsoleMode");
    
    	fdwMode = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
    	if (! SetConsoleMode(stdinInput, fdwMode) ) 
    			perror("SetConsoleMode"); 
    
    	initscr();
    	cbreak();
    
    	// Pad (pad) Properties
    	pad = newpad(100,80);
    	scrollok(pad,TRUE);
    	prefresh( pad, 0, 0, 0, 0, LINES-1, COLS-1);
    
    	// Input window (input_window) properties
    	input_window = newwin(height,width,starty,startx);
    	touchwin(input_window);
    	keypad(input_window,TRUE);
    	start_color();
    	init_pair(1,COLOR_WHITE, COLOR_RED);
    	wbkgd(input_window,COLOR_PAIR(1));
    	wmove(input_window,1,1);
    	wrefresh(input_window);
    
    
    	while(TRUE)
    	{
    		GetNumberOfConsoleInputEvents(stdinInput, &numEvents);
      
    	   if (numEvents != 0) {
        
    		   INPUT_RECORD eventBuffer;
        
    		  ReadConsoleInputA(stdinInput, &eventBuffer, 1, &numEventsRead);
     
              
    			if (eventBuffer.EventType == KEY_EVENT) {
    
    				if(eventBuffer.Event.KeyEvent.bKeyDown)
    				{
    					if(eventBuffer.Event.KeyEvent.wVirtualKeyCode != VK_PRIOR && eventBuffer.Event.KeyEvent.wVirtualKeyCode != VK_NEXT )
    					{
    					
    						if(wgetstr(input_window,dataBuffer) != ERR)
    						{
    							mvwprintw(pad,line,0,"%s\n",dataBuffer);
    							line++;
    							prefresh( pad, 0, 0, 0, 0, LINES-1,COLS-1);
    							werase(input_window);
    							wmove(input_window,1,1);
    
    						}
    					}
    
    					if(eventBuffer.Event.KeyEvent.wVirtualKeyCode == VK_PRIOR)
    					{
    						printw("Page Up Pressed\n");
    						refresh();
    					}
    
    					if(eventBuffer.Event.KeyEvent.wVirtualKeyCode == VK_NEXT)
    					{
    						printw("Page Down Pressed\n");
    						refresh();
    					}
    				
    					
    				}
    			}
         
    		}
    		Sleep(10);
    	}
    
    	//getch();
    	endwin();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing a bitmap when a button is pressed
    By h3ro in forum Windows Programming
    Replies: 5
    Last Post: 08-05-2008, 11:41 AM
  2. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  3. how do i check which button is pressed ?
    By intruder in forum Windows Programming
    Replies: 2
    Last Post: 04-24-2006, 03:24 PM
  4. How do i make out which button is pressed ?
    By intruder in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2006, 12:15 PM
  5. Replies: 1
    Last Post: 05-15-2004, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21