Skip to content

Commit

Permalink
Use scancode for key detection
Browse files Browse the repository at this point in the history
  • Loading branch information
eXpl0it3r committed Sep 19, 2024
1 parent 2719631 commit b3f0d3c
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/Desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main() {
while( render_window.pollEvent( event ) ) {
if(
(event.type == sf::Event::Closed) ||
(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
(event.type == sf::Event::KeyPressed && event.key.scancode == sf::Keyboard::Scan::Escape)
) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/GuessMyNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ int main() {
while( render_window.isOpen() ) {
while( render_window.pollEvent( event ) ) {
if(
(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) ||
(event.type == sf::Event::KeyPressed && event.key.scancode == sf::Keyboard::Scan::Escape) ||
event.type == sf::Event::Closed
) {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion include/SFGUI/Entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SFGUI_API Entry : public Widget {
void HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) override;
void HandleUpdate( float seconds ) override;
void HandleTextEvent( sf::Uint32 character ) override;
void HandleKeyEvent( sf::Keyboard::Key key, bool press ) override;
void HandleKeyEvent( sf::Keyboard::Key key, sf::Keyboard::Scancode scancode, bool press ) override;
void HandleSizeChange() override;
void HandleFocusChange( Widget::Ptr focused_widget ) override;

Expand Down
2 changes: 1 addition & 1 deletion include/SFGUI/SpinButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class SFGUI_API SpinButton : public Entry {
void HandleMouseButtonEvent( sf::Mouse::Button button, bool press, int x, int y ) override;
void HandleUpdate( float seconds ) override;
void HandleTextEvent( sf::Uint32 character ) override;
void HandleKeyEvent( sf::Keyboard::Key key, bool press ) override;
void HandleKeyEvent( sf::Keyboard::Key key, sf::Keyboard::Scancode scancode, bool press ) override;
void HandleSizeChange() override;
void HandleFocusChange( Widget::Ptr focused_widget ) override;

Expand Down
2 changes: 1 addition & 1 deletion include/SFGUI/Widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class SFGUI_API Widget : public Object, public std::enable_shared_from_this<Widg
* @param key Key.
* @param press true if button was pressed, false if released.
*/
virtual void HandleKeyEvent( sf::Keyboard::Key key, bool press );
virtual void HandleKeyEvent( sf::Keyboard::Key key, sf::Keyboard::Scancode scancode, bool press );

/** Handle widget (relative) position changes.
*/
Expand Down
16 changes: 8 additions & 8 deletions src/SFGUI/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ void Entry::HandleTextEvent( sf::Uint32 character ) {
}
}

void Entry::HandleKeyEvent( sf::Keyboard::Key key, bool press ) {
void Entry::HandleKeyEvent( sf::Keyboard::Key /*key*/, sf::Keyboard::Scancode scancode, bool press ) {
if( !press || !HasFocus() ) {
return;
}

switch( key ) {
case sf::Keyboard::BackSpace: { // backspace
switch( scancode ) {
case sf::Keyboard::Scan::Backspace: { // backspace
if( ( m_string.getSize() > 0 ) && ( m_cursor_position > 0 ) ) {
m_string.erase( static_cast<std::size_t>( m_cursor_position - 1 ) );

Expand All @@ -202,7 +202,7 @@ void Entry::HandleKeyEvent( sf::Keyboard::Key key, bool press ) {
GetSignals().Emit( OnTextChanged );
}
} break;
case sf::Keyboard::Delete: {
case sf::Keyboard::Scan::Delete: {
if( ( m_string.getSize() > 0 ) && ( m_cursor_position < static_cast<int>( m_string.getSize() ) ) ) {
m_string.erase( static_cast<std::size_t>( m_cursor_position ) );

Expand All @@ -224,22 +224,22 @@ void Entry::HandleKeyEvent( sf::Keyboard::Key key, bool press ) {
GetSignals().Emit( OnTextChanged );
}
} break;
case sf::Keyboard::Home: {
case sf::Keyboard::Scan::Home: {
if( m_string.getSize() > 0 ) {
m_visible_offset = 0;
SetCursorPosition( 0 );
}
} break;
case sf::Keyboard::End: {
case sf::Keyboard::Scan::End: {
if( m_string.getSize() > 0 ) {
m_visible_offset = 0;
SetCursorPosition( static_cast<int>( m_string.getSize() ) );
}
} break;
case sf::Keyboard::Left: {
case sf::Keyboard::Scan::Left: {
MoveCursor( -1 );
} break;
case sf::Keyboard::Right: {
case sf::Keyboard::Scan::Right: {
MoveCursor( 1 );
} break;
default: break;
Expand Down
6 changes: 3 additions & 3 deletions src/SFGUI/SpinButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ void SpinButton::HandleTextEvent( sf::Uint32 character ) {
}
}

void SpinButton::HandleKeyEvent( sf::Keyboard::Key key, bool press ) {
Entry::HandleKeyEvent( key, press );
void SpinButton::HandleKeyEvent( sf::Keyboard::Key key, sf::Keyboard::Scancode scancode, bool press ) {
Entry::HandleKeyEvent( key, scancode, press );

if( !press || !HasFocus() ) {
return;
}

if( key == sf::Keyboard::Return ) {
if( scancode == sf::Keyboard::Scancode::Enter ) {
GrabFocus( Widget::Ptr() );
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/SFGUI/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void Widget::HandleEvent( const sf::Event& event ) {
case sf::Event::KeyPressed:
if( HasFocus() ) {
// TODO: Delegate event too when widget's not active?
HandleKeyEvent( event.key.code, true );
HandleKeyEvent( event.key.code, event.key.scancode, true );
GetSignals().Emit( OnKeyPress );
}

Expand All @@ -446,7 +446,7 @@ void Widget::HandleEvent( const sf::Event& event ) {
case sf::Event::KeyReleased:
if( HasFocus() ) {
// TODO: Delegate event too when widget's not active?
HandleKeyEvent( event.key.code, false );
HandleKeyEvent( event.key.code, event.key.scancode, false );
GetSignals().Emit( OnKeyRelease );
}
break;
Expand Down Expand Up @@ -751,7 +751,7 @@ void Widget::HandleMouseMoveEvent( int /*x*/, int /*y*/ ) {
void Widget::HandleMouseButtonEvent( sf::Mouse::Button /*button*/, bool /*press*/, int /*x*/, int /*y*/ ) {
}

void Widget::HandleKeyEvent( sf::Keyboard::Key /*key*/, bool /*press*/ ) {
void Widget::HandleKeyEvent( sf::Keyboard::Key /*key*/, sf::Keyboard::Scancode /*scancode*/, bool /*press*/ ) {
}

void Widget::HandlePositionChange() {
Expand Down

0 comments on commit b3f0d3c

Please sign in to comment.