1

I am trying to build myself a simple screensaver in C++, with windows.h and PDCurses.

A fundemental part of screensavers is closing the screensaver when the mouse is moved, clicked, or the keyboard is pressed... etc.

I have tried to find a way for PDCurses to sense any mouse event, but with no avail, and the windows.h way of doing this is too complex for a C++ newb like me, and I can't think of a way to sense keyboard events either. Here is my code:

#include <windows.h>
#include <curses.h> //actually PDCurses is what I have.

int main()
{
    SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
    initscr();
    raw();
    keypad(stdscr, TRUE);
    noecho();
    curs_set(0);
    int doty = 1, dotx = 1, xm = 1, ym = 1, maxy, maxx;
    getmaxyx(stdscr, maxy, maxx);
    while(1){
        clear();
        mvaddch(doty - 1, dotx - 1, char(219));
        refresh();
        delay_output(35);
        if (doty >= maxy) ym = -1;
        if (dotx >= maxx) xm = -1;
        if (doty <= 1) ym = 1;
        if (dotx <= 1) xm = 1;
        dotx += xm;
        doty += ym;
    }
    endwin();
    return 0;
}

How would I go about detecting any keyboard or mouse events?

Eric Petersen
  • 77
  • 1
  • 7
  • Check this out: https://stackoverflow.com/questions/19875136/continuous-keyboard-input-in-c Hopefully it answers your question. – SavageGames24 Oct 18 '19 at 18:29
  • Thanks! so far I have implemented mouse move sensing, and keyboard input, with conio.h and windows.h, however, how would I sense the mouse buttons? – Eric Petersen Oct 20 '19 at 08:04

2 Answers2

0

There are limits to what events you can detect with PDCurses -- e.g., it won't report an event for mouse motion without a button being pressed. But, to capture all the events that you can -- add these lines before the while loop:

nodelay(stdscr, TRUE);
mouse_on(ALL_MOUSE_EVENTS);
PDC_return_key_modifers(TRUE);

and within the loop, add:

int c = getch();
if (c != ERR)
    break;

I tested this just now (minus the Windows lines), and got an error on the reference to char(219) -- I suggest replacing this with ACS_BLOCK. (That done, it worked as expected.)

Edit: I added PDC_return_key_modifers(TRUE) so that, e.g., the shift key will also exit the loop.

William McBrine
  • 2,166
  • 11
  • 7
0

Sorry for the late reply, as I gave you a link to a website that helped you with keyboard input and mouse sensing. For mouse button detection, check out this website:

https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-mousemove? redirectedfrom=MSDN

In your code, I think you could do (for the left mouse button being clicked):

if(0x0001)

{

// code

}

SavageGames24
  • 15
  • 1
  • 4