Skip to content

Instantly share code, notes, and snippets.

@JimHokanson
Created October 6, 2016 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimHokanson/c88192573930c87f1727f10d777d8890 to your computer and use it in GitHub Desktop.
Save JimHokanson/c88192573930c87f1727f10d777d8890 to your computer and use it in GitHub Desktop.
Calling back to Matlab with pressed keyboard button - see what.txt
You coded for 10 hours just to write this ???
function asdf(varargin)
try
disp(varargin{1})
catch
disp('error occurred')
end
%disp('I ran')
end
1) Clean up the wtf.cs - this thing is a mess
2) Add on documentation
asmInfo = NET.addAssembly('G:\repos\matlab_git\wtf.dll')
wtf = keyboard_listener.InterceptKeys(keyboard_listener.ProcCallback(@asdf));
This code listens for keys that have been pressed and calls a Matlab callback telling the callback what keys have been pressed.
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading.Tasks;
///Code based on:
///---------------------------------------
///http://null-byte.wonderhowto.com/how-to/create-simple-hidden-console-keylogger-c-sharp-0132757/
///Actual code:
///http://pastebin.com/nPmn7wAW
///Building the code
///------------------------------------
///http://www.mathworks.com/help/matlab/matlab_external/building-a-net-application-for-matlab-examples.html
///1) G: Changes the drive
///2) cd G:\repos\matlab_git
///
///http://stackoverflow.com/questions/6832496/command-prompt-how-to-add-a-set-path-only-for-that-batch-file-executing
///The ; doesn't seem to be needed
///3) SET PATH=%PATH%C:\Program Files (x86)\MSBuild\14.0\Bin
///
///
/// /t:libary => added when I gave an input to Main()
///4) csc /t:library /out:wtf.dll wtf.cs
///http://stackoverflow.com/questions/31408624/passing-matlab-methods-as-delegates-to-net-object
///http://www.mathworks.com/help/matlab/matlab_external/limitations-to-support-of-net-delegates.html
///System.Runtime.InteropServices
///
/// asmInfo = NET.addAssembly('G:\repos\matlab_git\wtf.dll')
///wtf = keyboard_listener.InterceptKeys(keyboard_listener.ProcCallback(@asdf));
///
namespace keyboard_listener{
////public delegate void ProcCallback(int nCode, IntPtr wParam, IntPtr lParam);
public delegate void ProcCallback(int fu);
///public delegate void AsyncCallback(IAsyncResult ar)
public class InterceptKeys
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
//public static int burger;
public static int sun;
////public static IntPtr burger;
////public static IntPtr sun;
public static ProcCallback wtf_batman;
///public static void Main(LowLevelKeyboardProc _proc)
///public static void Main()
public InterceptKeys(ProcCallback cheese)
////public InterceptKeys()
{
//var handle = GetConsoleWindow();
// Hide
//ShowWindow(handle, SW_HIDE);
wtf_batman = cheese;
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, IntPtr.Zero, 0);
//_hookID = SetHook(_proc);
//Application.Run();
//UnhookWindowsHookEx(_hookID);
}
~InterceptKeys()
{
Dispose();
}
public void Dispose()
{
if (_hookID != IntPtr.Zero){
UnhookWindowsHookEx(_hookID);
}
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
//burger = Marshal.ReadInt32(wParam);
//sun = Marshal.ReadInt32(lParam);
////burger = wParam;
////sun = lParam;
////Marshal.Copy(wParam,burger);
////Marshal.Copy(lParm,sun);
sun = Marshal.ReadInt32(lParam);
///_wtf_batman(nCode, wParam, lParam);
///_wtf_batman();
//I think this is needed
//Calling wtf_batman didn't seem to work
///This works ...
///Task taskA = new Task(() => wtf_batman());
///taskA.Start();
Task taskA = new Task(() => wtf_batman(sun));
taskA.Start();
//Task t = (wtf_batman).Start();
///Doesn't work
///Console.WriteLine("Testing");
///int vkCode = Marshal.ReadInt32(lParam);
///Console.WriteLine((Keys)vkCode);
///StreamWriter sw = new StreamWriter(Application.StartupPath+ @"\log.txt",true);
///sw.Write((Keys)vkCode);
///sw.Close();
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
///[DllImport("kernel32.dll")]
///static extern IntPtr GetConsoleWindow();
///[DllImport("user32.dll")]
///static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
///const int SW_HIDE = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment