From c2e62d6078782f25d1df18c56c0ae76c092e2aac Mon Sep 17 00:00:00 2001 From: Nathan Ostgard Date: Wed, 11 Nov 2015 06:40:51 -0800 Subject: [PATCH] Added support for arrays of key flags --- src/robotjs.cc | 103 ++++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/src/robotjs.cc b/src/robotjs.cc index 12e7a2d3..50f4fc30 100644 --- a/src/robotjs.cc +++ b/src/robotjs.cc @@ -420,43 +420,67 @@ int CheckKeyFlags(char* f, MMKeyFlags* flags) return 0; } +int GetFlagsFromString(v8::Handle value, MMKeyFlags* flags) +{ + v8::String::Utf8Value fstr(value->ToString()); + return CheckKeyFlags(*fstr, flags); +} + +int GetFlagsFromValue(v8::Handle value, MMKeyFlags* flags) +{ + if (!flags) return -1; + + // Optionally allow an array of flag strings to be passed + if (value->IsArray()) + { + v8::Local a = v8::Local::Cast(value); + for (uint32_t i = 0; i < a->Length(); i++) + { + v8::Local v(a->Get(i)); + if (!v->IsString()) return -2; + + MMKeyFlags f = MOD_NONE; + const int rv = GetFlagsFromString(v, &f); + if (rv) return rv; + + *flags = (MMKeyFlags)(*flags | f); + } + return 0; + } + + // If it's not an array, it should be a single string value + return GetFlagsFromString(value, flags); +} + NAN_METHOD(keyTap) { MMKeyFlags flags = MOD_NONE; MMKeyCode key; char *k; - char *f; - v8::String::Utf8Value fstr(info[1]->ToString()); v8::String::Utf8Value kstr(info[0]->ToString()); k = *kstr; - f = *fstr; switch (info.Length()) { case 2: + switch (GetFlagsFromValue(info[1], &flags)) + { + case -1: + return Nan::ThrowError("Null pointer in key flag."); + break; + case -2: + return Nan::ThrowError("Invalid key flag specified."); + break; + } break; case 1: - f = NULL; break; default: return Nan::ThrowError("Invalid number of arguments."); } - if (f) - { - switch(CheckKeyFlags(f, &flags)) - { - case -1: - return Nan::ThrowError("Null pointer in key flag."); - break; - case -2: - return Nan::ThrowError("Invalid key flag specified."); - break; - } - } - switch(CheckKeyCodes(k, &key)) { case -1: @@ -482,27 +506,34 @@ NAN_METHOD(keyToggle) bool down; char *k; char *f; - + //Get arguments from JavaScript. Nan::Utf8String kstr(info[0]); - Nan::Utf8String fstr(info[2]); //Convert arguments to chars. k = *kstr; - f = *fstr; - + //Check and confirm number of arguments. switch (info.Length()) { - case 3: - break; - case 2: - f = NULL; - break; - default: - return Nan::ThrowError("Invalid number of arguments."); + case 3: + //Get key modifier. + switch (GetFlagsFromValue(info[2], &flags)) + { + case -1: + return Nan::ThrowError("Null pointer in key flag."); + break; + case -2: + return Nan::ThrowError("Invalid key flag specified."); + break; + } + break; + case 2: + break; + default: + return Nan::ThrowError("Invalid number of arguments."); } - + //Get down value if provided. if (info.Length() > 1) { @@ -525,20 +556,6 @@ NAN_METHOD(keyToggle) } } - //Get key modifier. - if (f) - { - switch(CheckKeyFlags(f, &flags)) - { - case -1: - return Nan::ThrowError("Null pointer in key flag."); - break; - case -2: - return Nan::ThrowError("Invalid key flag specified."); - break; - } - } - //Get the acutal key. switch(CheckKeyCodes(k, &key)) {