[HELP][C#] System.BadImageFormatException when trying to access System.Windows.Input.Keyboard

Hellooooo,

I have a problem trying to access the System.Windows.Input.Keyboard class to be able to use any buttons ingame (and not just GTA’s controls).

Code Snippet
using System;
using System.Threading.Tasks;
using CitizenFX.Core;
using System.Windows.Input;

namespace InputManager
{
    public class InputManager : BaseScript
    {
        public InputManager() {
            Tick += OnTick;
        }

        private async Task OnTick() {
            await Delay(0);

            if (Keyboard.IsKeyDown(Key.Return)) {
                Debug.WriteLine("TEST");
            }
        }
    }
}
Error Stacktrace
[    679140] [    GTAProcess]             MainThrd/ Exception thrown by a task: System.AggregateException: One or more errors occurred. ---> System.BadImageFormatException: Error verifying InputManager.InputManager/<OnTick>d__1:MoveNext (): Cannot load method from token 0x0a000021 for call at 0x006d
[    679156] [    GTAProcess]             MainThrd/   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679156] [    GTAProcess]             MainThrd/   at InputManager.InputManager.OnTick () [0x0001f] in <2d7ba2b50ee8452a86f250601200c263>:0 
[    679171] [    GTAProcess]             MainThrd/   at CitizenFX.Core.BaseScript+<>c__DisplayClass34_1.<ScheduleTick>b__1 () [0x00030] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\BaseScript.cs:166 
[    679187] [    GTAProcess]             MainThrd/   at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679187] [    GTAProcess]             MainThrd/   at System.Threading.Tasks.Task.Execute () [0x00010] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679187] [    GTAProcess]             MainThrd/    --- End of inner exception stack trace ---

[    679187] [    GTAProcess]             MainThrd/ ---> (Inner Exception #0) System.BadImageFormatException: Error verifying InputManager.InputManager/<OnTick>d__1:MoveNext (): Cannot load method from token 0x0a000021 for call at 0x006d
[    679203] [    GTAProcess]             MainThrd/   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679203] [    GTAProcess]             MainThrd/   at InputManager.InputManager.OnTick () [0x0001f] in <2d7ba2b50ee8452a86f250601200c263>:0 
[    679203] [    GTAProcess]             MainThrd/   at CitizenFX.Core.BaseScript+<>c__DisplayClass34_1.<ScheduleTick>b__1 () [0x00030] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\BaseScript.cs:166 
[    679203] [    GTAProcess]             MainThrd/   at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679203] [    GTAProcess]             MainThrd/   at System.Threading.Tasks.Task.Execute () [0x00010] in <74fbbe963b7e417b8d715b858c5c584f>:0 <---
[    679218] [    GTAProcess]             MainThrd/ 

[    679218] [    GTAProcess]             MainThrd/ Failed to run a tick for InputManager: System.BadImageFormatException: Error verifying InputManager.InputManager/<OnTick>d__1:MoveNext (): Cannot load method from token 0x0a000021 for call at 0x006d
[    679218] [    GTAProcess]             MainThrd/   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679218] [    GTAProcess]             MainThrd/   at InputManager.InputManager.OnTick () [0x0001f] in <2d7ba2b50ee8452a86f250601200c263>:0 
[    679218] [    GTAProcess]             MainThrd/   at CitizenFX.Core.BaseScript+<>c__DisplayClass34_1.<ScheduleTick>b__1 () [0x00030] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\BaseScript.cs:166 
[    679218] [    GTAProcess]             MainThrd/   at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    679218] [    GTAProcess]             MainThrd/   at System.Threading.Tasks.Task.Execute () [0x00010] in <74fbbe963b7e417b8d715b858c5c584f>:0 

Any idea on what is causing that error? I even tried including WindowsBase.dll just to make sure it isn’t missing anything.

Are you sure that .dll library is the right .NET framework version? Should be .NET version 4.5.2 from memory

Yea. Well at least my script is. (it works fine when not using the Keyboard class)

Does it have to be the same for the library I am using? Because well… how do I find a WindowsBase.dll with 4.5.2 D:

Ahh yea from memory it does, well if you want to use the Newtonsoft.Json.dll on the client. It has to be version 4.5.2, and portable or some shit. No clue why tho, maybe you can find different versions of the ‘Keyboard’ library online?

COuldn’t find another version… but I am taking a look at other C# keyboard hooks.
I’ll update this thread if I can find something working :slight_smile:

https://cookbook.fivem.net/2020/01/06/using-the-new-console-key-bindings/

Wait a second… is this properly implemented yet? (from testing just now it works fine on one of the newer server builds)

In the cookbook post it says “partial implementation on canary”.
That is why I kind of wanted to get one running myself… Tbh didn’t even try this before.

And how would I go with adding controller keybindings if needed? (if that is even possible right now)

There’s likely an IOMS_* type you can specify for that, too.

Alright… that’s nice. Keyboard has priority though.
Really cool, that it is completely separate from controller input. Makes a lot more buttons accessible without cluttering the controller for those that use it.

But is the system fully implemented yet? In the cookbook it specifically says “In future versions of FiveM (currently a partial implementation is on canary)”. Maybe you can update the text in order to not confuse people (like me).

I want to add something for those who found this thread and are getting a BadImageFormatException and don’t know why:

Task.Delay() causes a BadImageFormatException too

Make sure you are not using Task.Delay() and use BaseScript.Delay() instead; it already happened me a few times that I used the former accidentally. These methods that cause that exception were probably restricted intentionally for the sandboxed scripting environment.

I have a problem BadImageFormatException I will pay you to fix it @ Sasino97