Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Kebinds to start (F5) and stop (F6) program #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ __clumsy makes your network condition on Windows significantly worse, but in a m

Leveraging the awesome [WinDivert](http://reqrypt.org/windivert.html), clumsy stops living network packets and capture them, lag/drop/tamper/.. the packets on demand, then send them away. Whether you want to track down weird bugs related to broken network, or evaluate your application on poor connections, clumsy will come in handy:

Use F5 to start and F6 to stop the program.

* No installation.
* No need for proxy setup or code change in your application.
* System wide network capturing means it works on any application.
Expand Down
52 changes: 50 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <time.h>
#include <Windows.h>
#include <conio.h>
#include "iup.h"
#include "common.h"

Expand Down Expand Up @@ -31,6 +32,7 @@ static Ihandle *timer;
static Ihandle *timeout = NULL;

void showStatus(const char *line);
static int KEYPRESS_CB(Ihandle *ih, int c, int press);
static int uiOnDialogShow(Ihandle *ih, int state);
static int uiStopCb(Ihandle *ih);
static int uiStartCb(Ihandle *ih);
Expand Down Expand Up @@ -119,6 +121,37 @@ EAT_SPACE: while (isspace(*current)) { ++current; }
}
}

LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )
{
char pressedKey;
// Declare a pointer to the KBDLLHOOKSTRUCTdsad
KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
switch( wParam )
{
case WM_KEYUP: // When the key has been pressed and released
{
//get the key code
pressedKey = (char)pKeyBoard->vkCode;
}
break;
default:
return CallNextHookEx( NULL, nCode, wParam, lParam );
break;
}

if(pressedKey == 116)
{
uiStartCb(NULL);
} else if(pressedKey == 117)
{
uiStopCb(NULL);
}
LOG("Character: %d", pressedKey);

//according to winapi all functions which implement a hook must return by calling next hook
return CallNextHookEx( NULL, nCode, wParam, lParam);
}

void init(int argc, char* argv[]) {
UINT ix;
Ihandle *topVbox, *bottomVbox, *dialogVBox, *controlHbox;
Expand Down Expand Up @@ -258,6 +291,11 @@ void init(int argc, char* argv[]) {
IupSetCallback(timeout, "ACTION_CB", uiTimeoutCb);
IupSetAttribute(timeout, "RUN", "YES");
}

//Retrieve the applications instance
HINSTANCE instance = GetModuleHandle(NULL);
//Set a global Windows Hook to capture keystrokes using the function declared above
HHOOK test1 = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, instance,0);
}

void startup() {
Expand Down Expand Up @@ -286,6 +324,10 @@ void showStatus(const char *line) {
IupStoreAttribute(statusLabel, "TITLE", line);
}

static int KEYPRESS_CB(Ihandle *ih, int c, int press){
LOG("Character: %d",c);
}

// in fact only 32bit binary would run on 64 bit os
// if this happens pop out message box and exit
static BOOL check32RunningOn64(HWND hWnd) {
Expand Down Expand Up @@ -359,7 +401,10 @@ static int uiOnDialogShow(Ihandle *ih, int state) {

static int uiStartCb(Ihandle *ih) {
char buf[MSG_BUFSIZE];
UNREFERENCED_PARAMETER(ih);
if(ih)
{
UNREFERENCED_PARAMETER(ih);
}
if (divertStart(IupGetAttribute(filterText, "VALUE"), buf) == 0) {
showStatus(buf);
return IUP_DEFAULT;
Expand All @@ -377,7 +422,10 @@ static int uiStartCb(Ihandle *ih) {

static int uiStopCb(Ihandle *ih) {
int ix;
UNREFERENCED_PARAMETER(ih);
if(ih)
{
UNREFERENCED_PARAMETER(ih);
}

// try stopping
IupSetAttribute(filterButton, "ACTIVE", "NO");
Expand Down