[b][u]C# .NET:[/u][/b]
I've got a console window, and I'm making a guy move around with WASD. How can I check what keys on the keyboard are pressed down just before my Draw() method?
KeyboardState keyboard = new KeyboardState();
keyboard.IsKeyDown(Keys.xxxxxxx)
[editline]10:21PM[/editline]
Not sure if it is KeyboardState or just Keyboard though.
Sorry to interrupt, but I'm having a very stupid Javascript problem. (Not posting in web development because it doesn't have a help thread.)
[code]
<html>
<head>
</head>
<form action="">
Username: <input type="text" name="user" /><br />
Password: <input type="password" name="pass" />
</form>
<form action="">
<input type="button" value="Sumbit" onClick="showuser(); showpass()">
</form>
<script type="text/javascript">
var getuservalue = document.getElementById("user");
var getpassvalue = document.getElementById("pass");
function showuser()
{
document.write("Your username is: " + getuservalue.value);
}
function showpass()
{
document.write("Your password is: " + getpassvalue.value);
}
</script>
</html>
[/code]
Just set that up as an example. But the problem I'm having is that it only runs the first function in the onClick function. It only says
Your username is: usernamehere
Missing a semi colon after showpass()
I'm by no means a Javascript expert, but I'm pretty sure that document.write() totally overwrites whatever is currently in the document. Calling it a second time will probably erase what you wrote the first time.
Well, he's calling showuser first and the code outputs only that so the second methods probably isn't even called.
The semicolon didn't do anything, I tested it. And if I was calling it a second time it would erase the first (username), but it's not.
@Spartan One: you're using getElementById, but you're only assigning the form fields names, not IDs.
I can tell that not even the first function got called properly, at least in Google Chrome, from the Developer Console. It says the result of getElementById is null. So that's how I figured that out.
(Earlier versions of IE will check for IDs or names, but this is not standard behaviour.)
Well it shows the usernames field correctly. If I hit the submit button with the above code it will show what you put in the username field but not the password field. It doesn't even say "Your password is: ". That is most likely not the problem.
Well, I'm trying to help you with Google Chrome, and on Google Chrome not even that works correctly. So fix that first and then I'll help you with the rest.
I'm trying it on Firefox and IE and the error Firefox gives me is this:
Error: getuservalue is null
Line: 16
[code]
<html>
<head>
</head>
<form action="">
Username: <input type="text" name="user" /><br />
Password: <input type="password" name="pass" />
</form>
<form action="">
<input type="button" value="Sumbit" onClick="showuser();showpass()">
</form>
<script type="text/javascript">
var getuservalue = document.getElementById("user");
var getpassvalue = document.getElementById("pass");
function showuser()
{
document.write("Your username is: " + getuservalue.value);
}
function showpass()
{
document.write("Your password is: " + getpassvalue.value);
}
</script>
</html>
[/code], I've edited it a tad, hasn't made a difference.
I just told you why that is happening. You need to assign the form fields an ID, NOT a name. id="user". id="pass"
Once you've fixed that, try changing the document.write to alert, and see if it works properly. You can't use document.write after the page has finished loading, as this will create a new page.
I just changed it to id="user" and same for the pass, and it still only says "Your username is: usernamehere", but it never even prints out or writes "Your password is: passwordhere".
Does it print that on a new page or the same one?
It stays with the same filename but it makes the whole page blank except for the "Your username is: usernamehere"
Yeah, that's why. Like I said, you can't run document.write after the page has already loaded, or else it creates a new one. Once this new page is created, there is no second function to run.
Well how should I fix this problem? Sorry if you stated the answer in a previous post.
[QUOTE=Spartan One;24400128]Well how should I fix this problem? Sorry if you stated the answer in a previous post.[/QUOTE]
Scroll up. :saddowns:
I saw your post, Robo, didn't know if it was correct.
Edited:
I don't really want to use the alert function, I would rather use a document.write function.
Create two empty <div>s, give them IDs, then use document.getElementById().innerHTML = whatever. Like this:
[code]<html>
<head>
</head>
<form action="">
Username: <input type="text" id="user" /><br />
Password: <input type="password" id="pass" />
</form>
<form action="">
<input type="button" value="Sumbit" onClick="showuser(); showpass(); return false;">
</form>
<div id="useroutput"></div>
<div id="passoutput"></div>
<script type="text/javascript">
var getuservalue = document.getElementById("user");
var getpassvalue = document.getElementById("pass");
var useroutputfield = document.getElementById("useroutput");
var passoutputfield = document.getElementById("passoutput");
function showuser()
{
useroutputfield.innerHTML = getuservalue.value;
}
function showpass()
{
passoutputfield.innerHTML = getpassvalue.value;
}
</script>
</html>[/code]
If I'm not mistaken, there are no global variables with javascript, all the variables must be stored within the functions and passed around. So just copy the getuservalue and useroutputfield into showuser() and getpassvalue and passoutputfield into showpass().
There are global variables.
Well, I figured it out, you guys inspired me to think of a way to do this:
[code]
<html>
<head>
</head>
<form action="">
Username: <input type="text" id="user" /><br />
Password: <input type="password" id="pass"/>
</form>
<form action="">
<input type="button" value="Sumbit" onClick="showcreds()">
</form>
<script type="text/javascript">
var getuservalue = document.getElementById("user");
var getpassvalue = document.getElementById("pass");
function showcreds()
{
document.write("Your username is: " + getuservalue.value + "<br />Your password is: " +getpassvalue.value);
}
</script>
</html>
[/code]
It prints out the text from the password box and the username box. Thanks alot guys!
[QUOTE=pikzen;24394417]KeyboardState keyboard = new KeyboardState();
keyboard.IsKeyDown(Keys.xxxxxxx)
[editline]10:21PM[/editline]
Not sure if it is KeyboardState or just Keyboard though.[/QUOTE]
XNA specific. Probably doesn't work with console windows, I bet.
So what I have right now:
[code]using System;
using System.Runtime.InteropServices;
namespace testkeystate
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern short GetAsyncKeyState(int vkey);
static void Main(string[] args)
{
while (true)
{
short state = GetAsyncKeyState(0x57); //according to msdn (http://msdn.microsoft.com/en-us/library/dd375731.aspx) this is the code of "W"
Console.WriteLine(state);
}
}
}
}[/code]
When I don't press W, it writes 0.
But... when I press W, it writes -32768.
What is the significance of this number? Is it the smallest possible short?
Why does it return this number? Why not just 1...?
Oh well, it works.
-32768 is -(2^15), the smallest possible value of a signed short int.
[editline]11:22PM[/editline]
make it an unsigned short if you want it to jump between 0 and 65535
[editline]11:23PM[/editline]
oh wait, I'm guessing it should be a boolean instead of an integer, since it's either pressed or not pressed...
The function is supposed to return the key's [b]state[/b], so in theory it could have more states, but -32768 is just a flag I guess.
Well, this number jumps around.
If pressed sometimes -32768, or -32767.
If not pressed, 0 or 1.
Why can't there be a simple library to grab all keyboard keys currently pressed and compose a char[] out of them or something?
Because it's easy enough to do it yourself. And if it jumps around I guess something is wrong - The few times I have used this function it changed only between 0 and -32768. So the safest way to check if key is down is to read the state like a flag value what can have different bits set.
.NET is driving me mad. Maybe one of you guys can help me out here.
I'm using reflection to grab a field that happens to be a delegate. I need to replace this delegate with my own, but the type of the delegate is private (so I can't create it from my method and assign it)
I have a delegate type with an exactly matching signature, so is there some way I can dynamically cast my delegate to this other type? I have a Type object representing the unknown type by the way.