首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JScript.Net中收听密钥

如何在JScript.Net中收听密钥
EN

Stack Overflow用户
提问于 2015-07-29 15:59:24
回答 1查看 331关注 0票数 1

我希望的是使用JScript .NET捕获按键,并使用该jsc.exe编译代码。那么,FLASH中是否有类似于"addEventListener("keyDown",keyCheck)“的内容。或GetAsyncKeyState()来自C++。我要用什么图书馆?请分享一个小的,简单的例子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-20 18:49:26

如果您正在编写控制台应用程序,这里有一个简单的解决方案。

代码语言:javascript
复制
import System;

Console.Write("Press the M key... ");

var key:ConsoleKeyInfo;

while (1) {
    while (!Console.KeyAvailable) {
        System.Threading.Thread.Sleep(1);
    }
    key = Console.ReadKey(1);
    if (key.Key == ConsoleKey.M) break;
}

Console.Write("Accepted.");

阅读更多关于ConsoleKeyInfo的信息。

如果您需要GetAsyncKeyState(),可以在JScript.NET中访问该方法。几天前,通过P/Invoke公开Win32 API方法的我遇到了一个JScript.NET函数。在这里,为了更简单的语法(允许传递API函数定义中的参数),对其进行了稍微的修改。

代码语言:javascript
复制
import System;
import System.Reflection;
import System.Reflection.Emit;

// Invoke a Win32 P/Invoke call.
// credit: http://cx20.main.jp/blog/hello/2013/03/07/hello-win32-api-jscript-net-world/
function InvokeWin32(dllName:String, returnType:Type, methodName:String, params:Object[]) {

    var paramTypes:Type[] = new Type[params.length];
    for (var i:int in params) {
        paramTypes[i] = params[i].GetType();
    }

    // Begin to build the dynamic assembly
    var domain = AppDomain.CurrentDomain;
    var name = new System.Reflection.AssemblyName('PInvokeAssembly');
    var assembly = domain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
    var module = assembly.DefineDynamicModule('PInvokeModule');
    var type = module.DefineType('PInvokeType',TypeAttributes.Public
        + TypeAttributes.BeforeFieldInit);

    // Define the actual P/Invoke method
    var method = type.DefineMethod(methodName, MethodAttributes.Public
        + MethodAttributes.HideBySig + MethodAttributes.Static +
        MethodAttributes.PinvokeImpl, returnType, paramTypes);

    // Apply the P/Invoke constructor
    var ctor = System.Runtime.InteropServices.DllImportAttribute.GetConstructor(
        [System.String]
    );
    var attr = new System.Reflection.Emit.CustomAttributeBuilder(ctor, [dllName]);
    method.SetCustomAttribute(attr);

    // Create the temporary type, and invoke the method.
    var realType = type.CreateType();
    return realType.InvokeMember(methodName, BindingFlags.Public + BindingFlags.Static
        + BindingFlags.InvokeMethod, null, null, params);
}

使用此函数,您可以使用以下语法公开Win32 DLL方法。(看到了吗?告诉你这更简单。)

代码语言:javascript
复制
// ShowWindowAsync(hWnd:IntPtr, nCmdShow:int);
function ShowWindowAsync(... args:Object[]):boolean {
   return InvokeWin32("user32.dll", System.Boolean, "ShowWindowAsync", args);
}

// GetWindowLong(hWnd:IntPtr, nIndex:int);
function GetWindowLong(... args:Object[]):int {
    return InvokeWin32("user32.dll", System.Int32, "GetWindowLong", args);
}

// FindWindowEx(parentHandle:IntPtr, childAfter:IntPtr,
//      lclassName:IntPtr, windowTitle:String);
function FindWindowEx(... args:Object[]):IntPtr {
    return InvokeWin32("user32.dll", System.IntPtr, "FindWindowEx", args);
}

我从未使用过GetAsyncKeyState(),但由于它是user32.dll方法,我猜它也会以同样的方式工作。(编辑:确实如此。)

代码语言:javascript
复制
// GetAsyncKeyState(vKey:int);
function GetAsyncKeyState(... args:Object[]):short {
    return InvokeWin32("user32.dll", System.Int16, "GetAsyncKeyState", args);
}

然后再举一个简单的例子:

代码语言:javascript
复制
import System;               // for Console methods
import System.Windows.Forms; // for Keys object constants

Console.Write("Press the M key... ");

// while the M key is not being pressed, sleep
while (!GetAsyncKeyState(Keys.M)) {
    System.Threading.Thread.Sleep(1);
}

// flush input buffer
while (Console.KeyAvailable) Console.ReadKey(1);

Console.WriteLine("Accepted.");
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31705530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档