So i was trying to detect the keywords with OpenTK and in their tutorial gave this code:
protected override void OnUpdateFrame(FrameEventArgs args)
{
//Get the state of the keyboard this frame
KeyboardState input = OpenTK.Input.Keyboard.GetState();
if (input.IsKeyDown(Key.Escape))
{
Exit();
}
base.OnUpdateFrame(args);
}
But what happens is that it gives an error at "Keyboard.GetState()". The error says: "The type or namespace name 'Keyboard' does not exist in the namespace 'OpenTK.Input' (are you missing an assembly reference?)".
Or if i dont add the 'OpenTK.Input', it wont let me import anything.
I have the same problem with "Key.Escape" and "Exit()"
Okey i solved it using KeyboardState.IsKeyDown(Keys.Space). With this i can take the space input. If any other key is wanted you change the the "Keys" class function.
An exemple with this would be:
protected override void OnUpdateFrame(FrameEventArgs args)
{
if (KeyboardState.IsKeyDown(Keys.Space))
{
Console.WriteLine("Somebody once told me");
}else if (KeyboardState.IsKeyDown(Keys.Escape))
{
this.Close();
}
base.OnUpdateFrame(args);
}
As you can see, the "Exit()" is replaced by "Close()" too!
Related
I am following this tutorial to learn how to use OpenGL in C#. Everything ran fine until this part: OpenGL 4 with OpenTK in C# Part 10: Asteroid Invaders. I am not using the exact same OpenTK as the one used in the tutorial. I am using this version that is compatible with .NET Core here: https://www.nuget.org/packages/OpenTK.NETCore/
Issue
Seems that OpenTK does not detect my keyboard. I have a simple update loop that handles keyboard input like this:
OnUpdateFrame
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
this.HandleKeyboard(e.Time);
// {...} Update Logic
}
HandleKeyboard
protected void HandleKeyboard(double delta)
{
Console.WriteLine("Handling keyboard");
var keyState = this.Keyboard.GetState();
if (keyState.IsKeyDown(Key.Escape))
{
this.Exit();
}
if (keyState.IsKeyDown(Key.A))
{
Console.WriteLine("Moving left");
this.player.MoveLeft();
}
if (keyState.IsKeyDown(Key.D) {
Console.WriteLine("Moving right");
this.player.MoveRight();
}
if (!this.gameOver && keyState.IsKeyDown(Key.Space) && this.lastKeyboardState.IsKeyUp(Key.Space))
{
this.gameObjects.Add(this.gameObjectFactory.CreateBullet(this.player.Position));
}
this.lastKeyboardState = keyState;
}
The console shows Handling keyboard many times, when I press A or D it doesn't write Moving left or Moving right. When I debug, and hover over keystate there is a property IsConnected = false. Maybe OpenTK is not recognizing my keyboard?
I am using the laptop keyboard so nothing fancy.
According to the documentation on Keyboard.GetState() it says Gets the primary Keyboard device, or null if no keyboard exists. But it doesn't return null?
How can I solve this? Thanks
Minimal example
I tried this on a separate solution:
using System;
using OpenTK;
using OpenTK.Input;
namespace OpenTKTest
{
class Program
{
[STAThread]
static void Main(string[] args)
{
using (var window = new GameWindow())
{
window.UpdateFrame += (sender, eventArgs) =>
{
var state = window.Keyboard.GetState();
if (state.IsKeyDown(Key.A))
{
Console.WriteLine("KEYSTATE");
}
};
window.KeyDown += (sender, eventArgs) =>
{
if (eventArgs.Key == Key.A)
{
Console.WriteLine("KEYDOWN EVENT");
}
};
window.Run(60);
}
}
}
}
And effectively, when I press the A key, KEYDOWN EVENT gets written but not KEYSTATE. So definitely a bug.
From what I gather (having experienced similar results), OpenTK.Input.Keyboard API is not yet implemented.
Errors: Invalid token If, UnityEngine.KeyCode is a 'method' but is used like a 'type', UnityEngine.KeyCode.Equals is a 'type' but is used like a 'Variable'
private void Update()
{
if (Input.GetKeyDown(KeyCode.I));
{
hideCheatGUI = true;
}
}
if (Input.GetKeyDown(KeyCode.Equals));
I need help fixing it. (I'm new so understand if I ask "noob" questions).
Simply remove the semicolons after your if statements. There you are not able to enter in any of those conditions.
Also, put your second if statement in your Update function. There, you put it outside.
private void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
hideCheatGUI = true;
}
if (Input.GetKeyDown(KeyCode.Equals))
{
// Your code
}
}
I'm trying to get OpenGL4Net working with C# in Microsoft Visual Studio Comunity 2015.
I've downloaded this file:
https://sourceforge.net/projects/ogl4net/files/Rev.%2037/x64/
And followed these instructions:
https://sourceforge.net/p/ogl4net/wiki/Tutorials/
At first with a console application but then starting again with a Windows Form application as it seems as if it was going to be using the window from that as opposed to making its own.
So far the various refrances have been added, form1.cs is untouched and Program.cs looks like this:
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenGL4NET;
namespace pads2
{
class Program : Form
{
RenderingContext rc;
static void Main(string[] args)
{
Program program = new Program();
program.Init();
Application.Run(program);
}
// required for open GL
void Init()
{
rc = RenderingContext.CreateContext(this);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
void Render()
{
gl.Clear(GL.COLOR_BUFFER_BIT);
// here is the right place to draw all your scene
rc.SwapBuffers();
}
// change window size
protected override void OnSizeChanged(EventArgs e)
{
gl.Viewport(0, 0, ClientSize.Width, ClientSize.Height);
// projection matrix may also need adjusting
}
// required for open GL
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case Windows.WM_PAINT: Render(); break;
default: base.WndProc(ref m); break;
}
}
}
}
/*
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
/*
The compiler seems unhappy about the comment at the end of the code, however the main issue is that I recieve the error:
The type or namespace name 'WM_PAINT' does not exist in the namespace 'Windows' (are you missing an assembly reference?)
I've been unable to find what reference I need for WM_PAINT online, including a reference for System.Windows did not help.
Q: How can I solve this and am I setting this up correctly?
WM_PAINT is described in detail in its MSDN entry:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd145213(v=vs.85).aspx
The article however omits its POD value, being the Integer constant "15".
Had this problem earlier, the example forgets to add the reference, the case should be:
case OpenGL4NET.Windows.WM_PAINT: Render(); break;
(Would comment if rep allowed me too)
I have downloaded a c# sql server ADO project i'm struggling to understand the part of this.dataGrid1.UnSelect(this.dataGrid1.CurrentRowIndex);
i dont see function definition for Unselect function, I cant figure out if its a user defined function or buildin function.
The error message is Systems.Windows.Forms.DataGridView does not contain definition for 'Unselect' and no extension method 'Unselect' accepting a first argument of type Sytems.Windows.Forms.DataGridView could be found (are you missing using directive or an assembly reference?)
private void btFirst_Click(object sender, System.EventArgs e)
{
if((currManager!=null)&(currManager.Count>0))
{
fnClearStatusbarPanel2();
fnSelectUnselectLastFirstRow(0);
//set the position at the first record 0
currManager.Position=0;
//enable pushbuttons Next, Last
fnEnableDisableButtons(this.btNext,this.btLast,"", true);
//disable pushbuttons First, Previous
fnEnableDisableButtons(this.btFirst, this.btPrevious,"First record...",false);
//display record numbers in the StatusBar
fnDisplayRecordNumbers();
}
}
private void fnSelectUnselectLastFirstRow (int posi)
{
//unselect the last selected/highlighted row
this.dataGrid1.UnSelect(this.dataGrid1.CurrentRowIndex);
//select the last or first row
this.dataGrid1.Select(posi);
}
I can’t figure out how get this method to work:
System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key)
The object browser says the following:
public static bool IsKeyDown(System.Windows.Input.Key key)
Member of System.Windows.Input.Keyboard
Summary:
Determines whether the specified key is pressed.
Parameters:
key: The specified key.
Return Values:
true if key is in the down state; otherwise, false.
Okay, so it’s a member of Keyboard, right? I used the following code:
Keyboard test = new Keyboard();
But when I type test and then the dot, IsKeyDown is not an option. The only options are from the Windows.Forms members. What am I missing here? Thanks.
Add PresentationCore.dll assembly as a reference.
Add WindowsBase.dll assembly as a reference.
Test code:
private void buttonMisc_Click(object sender, EventArgs e)
{
if (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift) == true)
MessageBox.Show("Got it!");
}
IsKeyDown is static, so you need to use it like
Keyboard.IsKeyDown()
Not with an instantiated object.
You also need to make sure you have the correct using statement at the top:
using System.Windows.Input;
EDIT
On further inspection, Keyboard is a static class... So you can't Keyboard test = new Keyboard();