1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: See if keys are pressed

Discussion in 'Software Development' started by doctorzeus, Dec 30, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    Hi,

    I'm still still kinda stretching my legs in the C/C++ field and was wondering whats the best way if you have and exe application running as a Windows service to detect if the keyboard has CTRL+Alt+F4 pressed down (Using VS and .NET framework so feel free to utilise .Net functions)?

    Also keep in mind this is running as a service so the smaller the resources used the better :)

    Any help would be great!

    Thanks

    Doctorzeus
     
  2. DaBeers

    DaBeers

    Joined:
    Oct 30, 2008
    Messages:
    276
    I am not a C++ guy, but I am familiar with .Net and keyboard handlers.

    It seems a little odd that a service would be coded to handle inputs. I believe that generally, these would be handled in the client, and passed as booleans to the service.

    Basically all you need to do is reference the io stream
    Code:
    [I]#include <iostream>[/I]
    
    You can then begin assigning inputs to variables, and outputs to the screen.

    This site has a good step-by-step on getting started with this.
    http://www.cplusplus.com/doc/tutorial/basic_io/

    GL,
     
  3. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    I should probubly re-phrase that :eek:, I want it to call a function when those keys are pushed down...

    Don't want it unessarily handling inputs :eek: less resources used the better

    I know some of the Iostream header but I probs should get more familiar with it, theres also <fstream> for files.

    Thanks

    Doctorzeus
     
  4. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    Ok this is what I've got:

    Code:
    void main()
    {
      unsigned int key;
      do
      {
            key = getch();
            printf("key = %d\n", key);
      }
      while(key != 'q');
      return 0;
    }
    
    However, does anyone know how to make this into a function that is called when you press down q or other key?

    e.g.

    Code:
    void keypress(arguments for calling keypress on keydown)
    {
    }
    Thanks

    Doctorzeus
     
  5. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    Anyone?

    I'm told theres also a better way perhaps utilising .NET...
     
  6. -Fabez-

    -Fabez-

    Joined:
    Jul 28, 2008
    Messages:
    1,899
    As it will be running as a service it might not always have focus. For this reason, try looking into hooks, which are capable of picking up keyboard events outside your programs scope and are only called when they detect a relevant event, meaning you don't have to use a while loop. This resource will help to get you started, but feel free to ask any questions.
     
  7. doctorzeus

    doctorzeus Thread Starter

    Joined:
    Jun 10, 2010
    Messages:
    277
    Ok thanks :)

    I started to consider getasynckeystate() but as you said that uses a loop so that sounds much better :D

    Thanks

    Doctorzeus
     
  8. -Fabez-

    -Fabez-

    Joined:
    Jul 28, 2008
    Messages:
    1,899
    No problems, hope it goes well :) Feel free to post back if you have any more questions. With the hook itself it will most likely have to be global, as it will need to detect events outside of it's scope, forgot to mention that in the last post.
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/971566

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice