hi, i am writing a program for my dad. I want it to keep the mouse in x=500 y=500 while waiting for input. So its asking for the password and if you try to move the mouse, it'll put the mouse back in x=500 and y=500 and have keep looping that until the correct password is inputed. Any solutions?

Try using the function SetCursorPos(int,int); (assuming Windows). You can use it with command prompt or Windows.
Edit: Dev-C++ is old, and I never used it. You should switch to more modern IDE which has a newer compiler.

Edited 5 Years Ago by sergent: n/a

I am using SetCursorPos(int,int). i just need it to loop forever until the right password is entered.

Oh, *facepalm* also use kbhit(), getch(). kbhit tells you if there is user input and getch gets the last character. (just google it). But I think they are only in conio.h and it is not stanard :(

Edited 5 Years Ago by sergent: n/a

Another good option is the ClipCursor function:

#include <windows.h>

int main()
{
    RECT old_rect; GetClipCursor(&old_rect);
    RECT new_rect = { 500, 500, 501, 501 };

    ClipCursor(&new_rect);

    Sleep(2500);

    ClipCursor(&old_rect);

    return 0;
}

Another good option is the ClipCursor function:

#include <windows.h>

int main()
{
    RECT old_rect; GetClipCursor(&old_rect);
    RECT new_rect = { 500, 500, 501, 501 };

    ClipCursor(&new_rect);

    Sleep(2500);

    ClipCursor(&old_rect);

    return 0;
}

this works great. i also need the program to run every sunday. Is there some type of timer that can run without the program running?

also, i need to disable the keyboard key that opens up the start menu and CTRL ALT DELETE.

Ok, so this is the example of my method, but m4ster_r0shi's method is good too :)

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <string>
#include <time.h>

using namespace std;

void sleep(clock_t wait)
{clock_t goal;
goal = wait + clock();
while(goal > clock())
;}

int main()
{
    bool correctPass = false;
    string password;
    int i = 0;
    while (correctPass == false) {
    SetCursorPos(500,500);
    if (kbhit()) { password = getch();}
        sleep(100);
        if (password == "a") correctPass = true;
        i++;
        }

        cout << password;
    return 0;
}

Hint: the password is "a"!!

also, i need to disable the keyboard key that opens up the start menu and CTRL ALT DELETE.

You cannot block CTRLD+ALT+DELETE, it is impossible on Windows machine, but you can probably block home key, look here. If you want your program to start every sunday, then just add it to registry and each time the computer starts, check if its sunday and if it is, then continue with the program if not then

return 0;

.

That link that you posted doesn't tell how to disable keys but I'll figure something out.

the code in that thread doesnt tell you how to disable the key i want to disable. The key i want to disable is the bottom 2nd left on the keyboard, the key with the windows sign on it. They were talking about the Home key on the six key pad above the arrow keys.

Oh there is a way to make your program run on a certain date/time, on W7 it's under:
Accessories -> System Tools -> Task Scheduler

It's a very useful Windows feature too :D

Also, I'm not yet convinced it's impossible to block ctrl + alt + delete, as soon as ctrl is detected the application can inject another keypress after it, ruining the combination. Another approach is to disable Windows' ability to actually run the alt+tab and ctrl+alt+del routines (maybe in a somewhat malicious way). What happens if you terminate explorer.exe when the user presses ctrl + alt + delete ? IDK, but it sounds fun!

Break out your windows hooks everyone!
http://msdn.microsoft.com/en-us/library/ms632589(v=vs.85).aspx

Note that I just assumed using a keyboard hook won't let you block ctrl + alt + del and alt + tab. I suppose if you can find a way to get your code into the address space of the process that does the ctrl + alt + del stuff then you can crash it, assuming Windows won't let you simply "terminate" it peacefully. But then again I suppose I could be entirely wrong.

Might want to block ctrl + shift + escape too, or better yet, all control keys (alt, control, etc.)

This link has some code and information about locking the desktop: http://www.codeproject.com/KB/winsdk/AntonioWinLock.aspx?df=100&forumid=62485&exp=0&select=1638161 it may be a bit outdated though.

Edited 5 Years Ago by pseudorandom21: n/a

ok, i am going to have the program open another program that i downloaded from your second link and i am wondering how to position a different window from the console window using SetWindowPos(). How would you?

that actually doesnt work. People say it doesnt work for console windows. is there a function that can move a console window?

Have you tried it? It works ok for me.

#define _WIN32_WINNT 0x0500

#include <windows.h>

bool IsPressed(int vkey)
{
    return GetAsyncKeyState(vkey) >> 15;
}

void MyMoveWindow(HWND hwnd, int dx, int dy)
{
    RECT wrect;

    GetWindowRect(hwnd, &wrect);

    MoveWindow(hwnd, wrect.left + dx, wrect.top + dy,
        wrect.right - wrect.left, wrect.bottom - wrect.top, TRUE);
}

int main()
{
    HWND hwnd = GetConsoleWindow();

    while (true)
    {
        if (IsPressed(VK_ESCAPE)) break;

        if (IsPressed(VK_UP))     MyMoveWindow(hwnd,   0, -10);

        if (IsPressed(VK_DOWN))   MyMoveWindow(hwnd,   0, +10);

        if (IsPressed(VK_LEFT))   MyMoveWindow(hwnd, -10,   0);

        if (IsPressed(VK_RIGHT))  MyMoveWindow(hwnd, +10,   0);

        Sleep(25);
    }

    return 0;
}

If you want to move a window other than your own, use HWND hwnd = FindWindow(0, "window name"); instead of HWND hwnd = GetConsoleWindow(); .

Edited 5 Years Ago by m4ster_r0shi: n/a

im just tring to get the program to move a secondary window to a certain position each time it runs and after the window is moved, have the mouse move and click on a button in that secondary window, and then switch back to the console window. The only problem is when i switch the task from the console to a secondary window, the mouse can move again.

heres what i have so far

#include<windows.h> 
#include<iostream>
#include<windows.h>
#include<commctrl.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string>
#include<vector>
using namespace std;
int main(char argc)
{
    RECT old_rect; GetClipCursor(&old_rect);
    RECT new_rect = {400, 400, 401, 401 };
    cout << "Enter password." << endl;
    ClipCursor(&new_rect);
   string pass = "";
   char ch;
   wrong:
   ch = _getch();
   while(ch != 13)
   {
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }
   if(pass == "thrustscc")
   {
      cout << "\nAccess granted :P\n";
      return 0;
   }
   else
   {
      cout << "\nAccess aborted...\n";
      goto wrong;
   }


    ClipCursor(&old_rect);

    system("pause");
    return 0;
}

when i press backspace, a new * appears on the screen. How do you make backspace apply in that? also i need the program to disable all task switching keys. Anybody know how?

Edited 5 Years Ago by thecoolman5: n/a

I would re-organize your includes to be (much more neat):

#include<windows.h> 
#include<commctrl.h>
#include<conio.h>

#include<cstdlib>
#include<cstdio>

#include<iostream>
#include<string>
#include<vector>

and your entry-point is wrong!! int main(char argc) should be int main(int argc, char *argv[]) I think maybe you should make a function to get the input character-by-character. It makes the code look neater and much easier to modify and extend if you use functional decomposition.

Basically you should read that codeproject article I posted, and be willing to put a little more work into the program, writing a windows hook is really not a difficult task and the other ideas in the article will be a fun learning experience too.

Edited 5 Years Ago by pseudorandom21: n/a

hey, do you know how to make the system output the home key? I cant figure out how to disable task switching keys. so, if someone presses the home key, i want the system to press that key again and restart the program. Do you know how to do that?

also, how can you make the system detect when you press the home key? im talking about the key that has the windows sign on it.

also, how can you make the system detect when you press the home key? im talking about the key that has the windows sign on it.

You can do that with a Windows hook! There is an example in my skydrive (the link in my signature).

This article has been dead for over six months. Start a new discussion instead.