- Home /
Turn numlock on at start
I've tried looking through previous answers, but can't find anything that quite fits the bill.
Basically, all I want is a script in void Start() just to turn numlock on. It doesn't matter if it's pressed, or if it was on in the first place, just need to make certain it is on at the start. I'm sure there must be a relatively simple code to do this. Just to note, I am using C#.
Cheers in advance,
Pete
Pako: Yeah, I found that aready, but I'm truely baffled by it. Tried to figure out devigod's post and work out how to alter it to do what I want, but couldn't get my head round it. Cyclops post, while also helpful, only allows for the numlock to be toggled, without regard for whether it is on or off in the first place... i think? Please currect me if I go that wrong, but I couldn't find a way to utilize it to my ends.
Eno $$anonymous$$haon: I'll check out those links.
Thankyou both for your guidance. I'm considering now whether it would be simpler (for the second at least) to either:
code the keys to work both with or without numlock. This may run into issues on different keyboard styles though.
or... add a note in this particular part of the game that numlock needs to be on. A crude solution, but it would work until I work out how to set it myself in the code. It would be nice however, if it would only display the message when numlock is off, which would require nearly the same kind of code I need to turn it on in the first place
Prefix the @ symbol to a name to (potentially) notify the user, like @AlwaysSunny.
Use comments to add follow-up information; using answers to provide non-answer information is ill advised. Best,
@iron_attorney actually it works like a charm! However, as the code stands in devigod's post, Special$$anonymous$$eyController is not a $$anonymous$$onoBehaviour, All I had to do to make it work as you want, is to make it a $$anonymous$$onoBehaviour, and then in Start() call SetNumLock(true). I've tried it, it works. All credit to @devigod (+1).
Answer by pako · Jun 11, 2015 at 11:45 AM
Check this out:
http://answers.unity3d.com/questions/19112/how-to-get-and-set-the-state-of-numlock.html
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class SpecialKeyController : MonoBehaviour {
// Use this for initialization
void Start () {
SetNumLock(true);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
private static extern short GetKeyState(int keyCode);
[DllImport("user32.dll")]
private static extern int GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll", EntryPoint = "keybd_event")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private const byte VK_NUMLOCK = 0x90;
private const uint KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 0x2;
private const int KEYEVENTF_KEYDOWN = 0x0;
public bool GetNumLock()
{
return (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
}
public void SetNumLock(bool bState)
{
if (GetNumLock() != bState)
{
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYDOWN, 0);
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
@pako cheers very much for copying that out! And fair play @devigod for this good work. Does doing that (+1) thing increase the users karma? Incase it does: @devigod(+1) @pako(+1). I had totally missed the top section, so I didn't include this line before:
using System.Runtime.InteropServices;
Now I got that working, I reckon it won't be too hard to make numlock stay on for the whole scene too. As a further thought, user32.dll is windows specific isn't it? I'm assu$$anonymous$$g I'll have to research the individual libraries for each platform to make this code portable.
Cheers again everyone for your help! Pete
(+1) is just an indication that an answer or comment will be up-voted, which then increases karma.
I guess that if you put the SetNumLock(true) instruction inside update, it would keep the numlock 'on' for as long as you want. But maybe you should consider calling the method every, say 100 frames or more, for better performance. Or maybe you could call it just before any of your code depends on NumLock being 'on'.
user32.dll contains Windows native code, i.e. non-dotNet code, a.k.a. unmanaged code. This is why you need using System.Runtime.InteropServices; at the top. So you can call unmanaged code from managed code (e.g. C#). I'm not 100% sure about what I'm going to say, cause maybe I'm missing something, but I would thing that this code is portable as is, as long as it is within Unity. In other words, I think that the Special$$anonymous$$eyController class would run on all Unity-supported platforms. Try it and see.
As you said, user32.dll is part of Windows. I would be very surprised if this code was portable to other platforms. And what about keyboards that don't have a numeric keypad?
I thought I might be missing something... So, I guess only managed code dlls in Unity are cross platform, right?
@Dave Carlile I guess people without a numpad can't play my game :D or I'll have to add some options for that eventuality...
@pako I'd imaging you are indeed right. I'll have to research managed dll alternatives to directly accessing user32. I'd guess there's probably plentiful resources on this subject on the net already. I'll leave that as a job for later. The code works on my development machine, so I can keep it as is for now.
Thanks again for your help guys! $$anonymous$$uch apppreciated
Pete
Answer by Eno-Khaon · Jun 11, 2015 at 09:29 AM
I would recommend starting here for information on this topic. Keyboard manipulation would be something the OS (Windows, in the case of the link) would handle, rather than a game engine. I don't know what may or may not be usable in Unity, but that's probably a good place to start.
Edit: Since the links from that site weren't great, here's another reference to "keybd_event".
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Accessing keys 1-9 All at Once 1 Answer
Questions about numbers, C# 4 Answers
C# Number to binary 1 Answer