Solved: See if keys are pressed

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 
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,
 

doctorzeus

Thread Starter
Joined
Jun 10, 2010
Messages
277
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,
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
 

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
 

doctorzeus

Thread Starter
Joined
Jun 10, 2010
Messages
277
Anyone?

I'm told theres also a better way perhaps utilising .NET...
 
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.
 

doctorzeus

Thread Starter
Joined
Jun 10, 2010
Messages
277
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.
Ok thanks :)

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

Thanks

Doctorzeus
 
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top