Hooking CTRL pressed in GTK+ application - hook

How can I hook CTRL pressed in "drawingarea_button_press_event_cb" function of GTK+ application?
void drawingarea_button_press_event_cb( GtkWidget *widget, GdkEventButton *event )
{
........
}

Inspect the state field of the GdkEventButton structure. It will have the GDK_CONTROL_MASK bit set if Control is being held down:
if(event->state & GDK_CONTROL_MASK)
printf("You're totally in control!\n");

Related

How do you listen for keyboard events in WinUI?

Real simple issue: I want to know if the CTRL key is pressed when the user sorts a ListView. If the CTRL key is down, then I want to extend the number of columns in the sort. if the CTRL key is up, then I just sort on the selected column (no, this isn't a DataGrid, just a ListView with a set of header controls).
I found this, but it doesn't work (Window.Current == null) in the constructor.
public PositionView(PositionViewModel positionViewModel)
{
this.DataContext = positionViewModel;
Window.Current.CoreWindow.KeyDown += this.CoreWindow_KeyDown;
this.InitializeComponent();
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
throw new NotImplementedException();
}
I don't want to get notified of a keyboard event just from the control with the input focus, I want notification for the entire application.
You can try this way:
static bool IsKeyDown(VirtualKey key)
{
return InputKeyboardSource
.GetKeyStateForCurrentThread(key)
.HasFlag(CoreVirtualKeyStates.Down);
}

Enable/Disable Edit box with help of Check-box control (MFC)

I have a checkbox and an edit control. I want to disable the Edit control when Check-box is 'not checked', and enable Edit control when Check-box is 'checked.
OnBnClickedCheck1 gets called when I check/uncheck the check-box. m_CHECK1_VARIABLE tells me if the check box is checked or un-checked. If-else part is executed correctly but m_TEXT1_CONTROL.EnableWindow(FALSE/TRUE) doesn't seem to work.
Below is the code.
void CPreparationDlg::OnBnClickedCheck1()
{
UpdateData(TRUE);
if (m_CHECK1_VARIABLE)
{
m_TEXT1_CONTROL.EnableWindow(TRUE);
}
else if (m_CHECK1_VARIABLE)
{
m_TEXT1_CONTROL.EnableWindow(FALSE);
}
}
There are 2 cases.
When Edit-box is disabled by default when dialog pops up.
If the Edit-box is enabled by default (I set 'Disabled' behavior in dialog properties to 'False'), Edit-box stays enabled throughout the operation. (check and uncheck operation on check-box)
When Edit-box is enabled by default when dialog pops up.
When I disable the Edit-box by default (I set 'Disabled' behavior in dialog properties to 'True'), Edit-box becomes enabled on 'first' 'check' on the Check-box but stays enabled throughout the rest of the operation. (check and uncheck operation on check-box).
What is it that I am missing here?
The following code example will implement the required logic.
Header file:
public:
int m_Check;
CEdit m_EditBox;
afx_msg void OnBnClickedCheck1();
Class implementation source:
CMfcApplicationDlg::CMfcApplicationDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMfcApplicationDlg::IDD, pParent)
, m_Check(0) // Default checkbox state
{
// ...
}
void CMfcApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_EditBox);
DDX_Check(pDX, IDC_CHECK1, m_Check);
m_EditBox.EnableWindow(m_Check);
}
void CMfcApplicationDlg::OnBnClickedCheck1()
{
UpdateData();
}
All required functionality can be implemented inside the DoDataExchange() method. First time the edit box control state set according to the m_Check default value. And each next time the edit box control state will be triggered by OnBnClickedCheck1() event.
IMHO using DoDataExchange(..) to maintain the state of a dialog is dicey at best. Add a member like UdateState( ) and use that. Stage the dialog in OnInitDialog( ) with anything that doesn't easily initialize in the constructor and call UpdateState( ).
Use DoDataExchange(..) only to do what it sounds like, exchange data between the dialog and objects. This way you won't paint yourself into a corner as the Dialog evolves.
//....h
CEdit m_EditBox;
CButton m_CheckBox;
//...cpp
BOOL MyDialog::OnInitDialog( )
{
if( ! CDialogEx::OnInitDialog( ) )
return FALSE;
//do more stuff then
UpdateState( );
return TRUE;
}
void MyDialog::UpdateState( )
{
m_EditBox.EnableWindow( m_CheckBox.GetCheck( ) == BST_CHECKED );
//more state stuff...
}
void MyDialog::OnBnClickedCheck1( )
{
UpdateState( );
}

MFC VC++ CStatic only receiving WM_NCHITTEST

I have created a small MFC Document View App in C++ and I am having some trouble receiving messages in a class that inherits from CStatic. I have managed to create the CStatic derivative and it is visible on my View however my message handlers are not being fired.
When using Spy++ it seems that window is only receiving WM_NCHITTEST and it is returning HTTRANSPARENT, which according to MSDN means:
"In a window currently covered by another window in the same thread (the message will be sent to underlying windows in the same thread until one of them returns a code that is not HTTRANSPARENT)."
Here is an exert from Spy++:
<000001> 001D1350 S WM_NCHITTEST xPos:128 yPos:167
<000002> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
<000003> 001D1350 S WM_NCHITTEST xPos:128 yPos:166
<000004> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
<000005> 001D1350 S WM_NCHITTEST xPos:128 yPos:165
<000006> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
<000007> 001D1350 S WM_NCHITTEST xPos:128 yPos:164
<000008> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
This seems strange because the CStatic derivative is the only child window of my view. I created it like this:
Create(pItem->Value->GetBuffer(), WS_CHILD | WS_VISIBLE | SS_CENTER, Rect, Parent);
ShowWindow(SW_SHOW);
where Parent is a pointer to the CView.
Any help would be really appreciated.
EDIT:
Foo.h
class Foo: public CStatic
{
DECLARE_DYNAMIC(Foo)
public:
Foo();
virtual ~Foo();
virtual void CreateCtrl(CWnd * Parent, POINT TopLeft, SIZE sz);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
Foo.cpp
void Foo::CreateCtrl(CWnd * Parent, POINT TopLeft, SIZE sz)
{
CRect Rect(TopLeft, sz);
Create(pItem->Value->GetBuffer(), WS_CHILD | WS_VISIBLE | SS_CENTER, Rect, Parent);
ShowWindow(SW_SHOW);
}
BEGIN_MESSAGE_MAP(Foo, CStatic)
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
void Foo::OnLButtonUp(UINT nFlags, CPoint point)
{
AfxMessageBox("Hello World!");
__super::OnLButtonUp(nFlags, point);
}
See Microsoft's article "About Static Controls", and in particular this part:
WM_NCHITTEST: Returns HTCLIENT if the control style is SS_NOTIFY; otherwise, returns HTTRANSPARENT.
Once the window returns HTTRANSPARENT from WM_NCHITTEST, all further mouse messages go to the window underneath it in Z-order; in your case, the parent view. The window is "transparent" as far as mouse handling is concerned.
After a bit of experimenting, it looks like setting and additional SS_NOTIFY style in Foo::CreateCtrl() gets MFC to call Foo::OnLButtonUp().
I am a bit confused with this style setting, specially after reading this SO post; the MSDN page for SS_NOTIFY just says "Sends the parent window STN_CLICKED, STN_DBLCLK, STN_DISABLE, and STN_ENABLE notification codes when the user clicks or double-clicks the control."
Maybe without the SS_NOTIFY style, it doesn't have to receive messages because they are not relayed to the parent?
Anyway, adding the SS_NOTIFY style seems to make it work!

How to access the Keyboard in an Eclipse RCP / LWJGL app?

I am working my way through the NeHe OpenGL examples, using the LWJGL for the OpenGL binding inside an Eclipse RCP application.
My OpenGL graphics are displayed inside the RCP canvas, not in a separate window.
Lesson 07 shows how to use the keyboard. If I try to do a:
Keyboard.create();
I get an error that the (OpenGL) "Display" has not been created.
If I create an OpenGL "Display" with org.lwjgl.opengl.Display.create(), then I get a new Window.
So how do I access the Keyboard without creating a new Window?
You cannot use the Keyboard without a Display, because of how LWJGL works behind the scenes. The best way is to just use AWT events. You can write your own input class, that could go something like this.
public class Input implements KeyListener {
private boolean aDown; //is the A key down?
//Ect, for all needed keys
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = true; break;
//and so on for all other needed keys.
}
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = false; break;
//and so on for all other needed keys.
}
}
public void keyTyped(KeyEvent ke) {} //Do nothing
public void isADown() {return aDown;}
}

MFC menu item checkbox behavior

I'm trying to add a menu item such that it acts like a check mark where the user can check/uncheck, and the other classes can see that menu item's check mark status. I received a suggestion of creating a class for the menu option (with a popup option), however, I can't create a class for the menu option when I'm in the resource layout editor in Visual Studio 2005. It would be great to hear suggestions on the easiest way to create menu items that can do what I have described.
You should use the CCmdUI::SetCheck function to add a checkbox to a menu item, via an ON_UPDATE_COMMAND_UI handler function, and the ON_COMMAND handler to change the state of the checkbox. This method works for both for your application's main menu and for any popup menus you might create.
Assuming you have an MDI or SDI MFC application, you should first decide where you want to add the handler functions, for example in the application, main frame, document, or view class. This depends on what the flag will be used for: if it controls application-wide behaviour, put it in the application class; if it controls view-specific behaviour, put it in your view class, etc.
(Also, I'd recommend leaving the menu item's Checked property in the resource editor set to False.)
Here's an example using a view class to control the checkbox state of the ID_MY_COMMAND menu item:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
afx_msg void OnMyCommand();
afx_msg void OnUpdateMyCommand(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
ON_UPDATE_COMMAND_UI(ID_MY_COMMAND, OnUpdateMyCommand)
END_MESSAGE_MAP()
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
// Use the new flag value.
}
void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_Flag);
}
You should ensure the m_Flag member variable is initialised, for example, in the CMyView constructor or OnInitialUpdate function.
I hope this helps!
#ChrisN's approach doesn't quite work for MFC Dialog applications (the pCmdUI->SetCheck(m_Flag); has no effect). Here is a solution for Dialog apps:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
CMenu * m_menu;
virtual BOOL OnInitDialog();
afx_msg void OnMyCommand();
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
END_MESSAGE_MAP()
BOOL CMyView::OnInitDialog()
{
m_menu = GetMenu();
}
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
if (m_flag) {
m_menu->CheckMenuItem(ID_MENUITEM, MF_CHECKED | MF_BYCOMMAND);
} else {
m_menu->CheckMenuItem(ID_MENUITEM, MF_UNCHECKED | MF_BYCOMMAND);
}
}
References:
http://www.codeguru.com/forum/showthread.php?t=322261
I ended up retrieving the menu from the mainframe using GetMenu() method, and then used that menu object and ID numbers to call CheckMenuItem() with the right flags, as well as GetMenuState() function.

Resources