GetAsyncKeyState

GetAsyncKeyState

Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Well I'm working on a program in C++, and I'm using GetAsyncKeyState to record the keys pressed. For the most part its working, but I've got one line that for some reason won't work:
C++ code
else if (key_stroke == VK_CONTROL)
fprintf(OUTPUT_FILE, "%s", "[CONTROL]");



I have lines similar to that one that work fine for other keys (shift, enter, arrow keys, etc), but for whatever reason I can't get it to recognize the control key at all.

Also is there a good alternative to GetAsyncKeyState that would work on all systems?
 
 
2010 Nov 26 at 08:46 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Well, control decided to magically start working (Sadly I don't know what I did to fix it ). But I'd still like to know if there is a good alternative to GetAsyncKeyState, preferably one that'd work on windows/linux/max
 
 
2010 Nov 26 at 09:17 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Rockbomb said:
But I'd still like to know if there is a good alternative to GetAsyncKeyState, preferably one that'd work on windows/linux/max

Here's a great way to record keys that works with windows/mac/linux/everything else. Setup a video camera behind the computer aimed at the keyboard and have it send the feed wirelessly to the receiver in your van parked outside. Yes, that was on Law and Order (SVU).
 
 
2010 Nov 26 at 10:02 PST
SuperJer
Websiteman

2005 Mar 20 • 6155
I always use SDL which works on every platform. It may not be the right choice for your project though.

I don't really know what other options there are for cross-platform input like that.

Here's a small example SDL program with Key State checking (near the very bottom):

c code
#include <stdio.h>
#include <SDL/SDL.h>

#define WIDTH 800
#define HEIGHT 600
#define BPP 4
#define DEPTH 32


// Set the color of a single pixel on a LOCKED surface
void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 color = SDL_MapRGB( screen->format, r, g, b );
*((Uint32*) screen->pixels + y*screen->pitch/BPP + x) = color;
}


// Draw some crazy trippy BS on the screen
void Draw(SDL_Surface* screen, int frameno)
{
int x, y, r, g, b, fmod = frameno%256;
if(SDL_MUSTLOCK(screen) && SDL_LockSurface(screen)<0 )
return;
for( y = 0; y < screen->h; y++ )
for( x = 0; x < screen->w; x++ )
{
r = (x>100&&x<500) ? (y*fmod*x*fmod/330000) : 0;
g = ((x*x+y*y+frameno*5)%256)/5;
b = (x>300&&x<700) ? (y*fmod*x*fmod/24000) : 0;
setpixel(screen, x, y, r, g, b);
}
if(SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
SDL_Flip(screen);
}


int main(int argc, char* argv[])
{
SDL_Surface *screen;
SDL_Event event;
int quitting = 0;
int frameno = 0;

if (SDL_Init(SDL_INIT_VIDEO) < 0 ) { return 1; }
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE))) { SDL_Quit(); return 1; }

while(!quitting)
{
Draw(screen,frameno++);
while(SDL_PollEvent(&event))
switch (event.type)
{
case SDL_QUIT:
quitting = 1;
break;
}

// Keyboard state!
Uint8 *keystate = SDL_GetKeyState(NULL);
if( keystate[SDLK_LCTRL] && keystate[SDLK_RCTRL] )
printf("Left and Right Control Keys Pressed.\n");
else if( keystate[SDLK_LCTRL] )
printf("Left Control Key Pressed.\n");
else if( keystate[SDLK_RCTRL] )
printf("Right Control Key Pressed.\n");
}

SDL_Quit();
return 0;
}


You need the SDL development files installed (e.g. sudo apt-get install libsdl-dev on Ubuntu)

I compiled it with: gcc -lSDL example.c
 
 
2010 Nov 26 at 11:14 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
That looks pretty good, I'll try it out. I think I'll wait 'til my next project (or possibly a v2.0 of this one), cuz I've already used quit a bit of windows-only functions... don't wanna go through and change all of them to make it cross platform

My next question is, whats a good library to use for FTP? I was looking at libcurl, but it looks like thats a C library (would work, but idk C )
 
 
2010 Nov 26 at 15:14 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
You should use sftp instead its 'better.'
ibcurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos4), file transfer resume, http proxy tunneling and more!

libcurl is highly portable, it builds and works identically on numerous platforms, including Solaris, NetBSD, FreeBSD, OpenBSD, Darwin, HPUX, IRIX, AIX, Tru64, Linux, UnixWare, HURD, Windows, Amiga, OS/2, BeOs, Mac OS X, Ultrix, QNX, OpenVMS, RISC OS, Novell NetWare, DOS and more...

Chilkat C++ SFTP Library+ code
#include <CkSFtp.h>

void ChilkatSample(void)
{
// Important: It is helpful to send the contents of the
// sftp.LastErrorText property when requesting support.

CkSFtp sftp;

// Any string automatically begins a fully-functional 30-day trial.
bool success;
success = sftp.UnlockComponent("Anything for 30-day trial");
if (success != true) {
printf("%s\n",sftp.lastErrorText());
return;
}

// Set some timeouts, in milliseconds:
sftp.put_ConnectTimeoutMs(5000);
sftp.put_IdleTimeoutMs(10000);

// Connect to the SSH server.
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
long port;
const char * hostname;
hostname = "www.my-ssh-server.com";
port = 22;
success = sftp.Connect(hostname,port);
if (success != true) {
printf("%s\n",sftp.lastErrorText());
return;
}

// Authenticate with the SSH server. Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication. This example uses password authenication.
success = sftp.AuthenticatePw("myLogin","myPassword");
if (success != true) {
printf("%s\n",sftp.lastErrorText());
return;
}

// After authenticating, the SFTP subsystem must be initialized:
success = sftp.InitializeSftp();
if (success != true) {
printf("%s\n",sftp.lastErrorText());
return;
}

// Open a file for writing on the SSH server.
// If the file already exists, it is overwritten.
// (Specify "createNew" instead of "createTruncate" to
// prevent overwriting existing files.)
const char * handle;
handle = sftp.openFile("hamlet.xml","writeOnly","createTruncate");
if (handle == 0 ) {
printf("%s\n",sftp.lastErrorText());
return;
}

// Upload from the local file to the SSH server.
success = sftp.UploadFile(handle,"c:/temp/hamlet.xml");
if (success != true) {
printf("%s\n",sftp.lastErrorText());
return;
}

// Close the file.
success = sftp.CloseHandle(handle);
if (success != true) {
printf("%s\n",sftp.lastErrorText());
return;
}

printf("Success.\n");
}


http://www.chilkatsoft.com/ssh-sftp-c++.asp
 
 
2010 Nov 26 at 15:35 PST — Ed. 2010 Nov 26 at 15:42 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
SFTP is just more secure, as far as I know. And I really don't care how secure it is, as I'm only writing the program for the sakes of writing a tutorial on it. I'll include in the tutorial that SFTP is recommended.
 
 
2010 Nov 26 at 15:43 PST
SuperJer
Websiteman

2005 Mar 20 • 6155
If you know C++ you shouldn't have any trouble using a C library in it. All you'll need to do is call the library functions you need, which works the same as calling C++ functions. There's no methods (class member functions) in a C library, unless you can find a C++ wrapper for it. But that probably wouldn't even change much anyway.
 
 
2010 Nov 27 at 12:15 PST — Ed. 2010 Nov 27 at 12:16 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
So I'd still use all the same C++ syntax, right?
Also, do you have any preference to a specific ftp library?
 
 
2010 Nov 27 at 12:17 PST — Ed. 2010 Nov 27 at 12:17 PST
SuperJer
Websiteman

2005 Mar 20 • 6155
Yeah it's the same syntax.

libcurl is actually my favorite. It's really wordy but it's the only one I've used that can actually do everything I need.

The other ones I use are like wget and whatever PHP has.

Also FTP sucks. Don't use it unless you have to. It's insecure and sometimes silently corrupts your transfer so the copied file gets mangled and you don't even know it.
 
 
2010 Nov 27 at 12:25 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
What would you suggest over ftp?
 
 
2010 Nov 27 at 12:30 PST
SuperJer
Websiteman

2005 Mar 20 • 6155
It depends on what you're trying to do. But pretty much everything other than FTP is secure and reliable. Examples: SFTP, SCP, HTTP (not secure), HTTPS, rsync, NFS (or similar), or even email.

If you control both sides then all of these are options. If you don't, you have to use what is available. That's why I said don't use FTP unless you have to.
 
 
2010 Nov 27 at 12:41 PST
Rockbomb
Dog fucker (but in a good way now)

2009 Nov 13 • 2045
Hmmm, I'll probably go with SFTP then... or possibly http.
 
 
2010 Nov 27 at 13:05 PST
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
The real question is:
What is the fastest, most secure, least problematic way to transfer files to a remote host?
 
 
2010 Nov 27 at 13:48 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2010 Nov 27 at 15:52 PST
SuperJer
Websiteman

2005 Mar 20 • 6155
sprinkles said:
The real question is:
What is the fastest, most secure, least problematic way to transfer files to a remote host?

There's not really one answer to that.

Is the host already running an HTTPS server? A SFTP server? rsync? An email server that's easy to plugin to? Can the client just do a network mount and pretend to copy files locally? Maybe the client and server already have a Windows share? Maybe you can't install whatever you want on the server?

Then there's the logical questions: should files be overwritten? What are permissions like? Do files need weird names? New names? How big are they? Do they need locks? Blah blah blah.

Depending on the answers to these questions and more, just about anything could end up being the best choice TM.
 
 
2010 Nov 27 at 20:17 PST
phoenix_r

2009 May 13 • 896
17 ₧
Down Rodeo said:
IPOAC - IP over avian carrier.

::fist-bump:: RFC 1149 is *legit*.
BOO
 
 
2010 Dec 3 at 14:53 PST
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
2010 Dec 3 at 17:04 PST
Page [1]